2017-10-26 4 views
0

Ich versuche, eine NSUmage zu verkleinern, um es mit Parse als PFFile zu verwenden, und ich muss seine Dimension ändern, um seine Datengröße zu reduzieren, aber es funktioniert nicht. Das skalierte Bild passt die Breite und Höhe innerhalb von Swift an, behält jedoch die gleichen Abmessungen wie das ursprüngliche Bild bei.Swift - OSX - NSAmage-Datengröße ändern

Das Bild ist ein JPG mit 2560 x 1706 und 721 KB.

Wenn ich es Daten passieren hält es die gleichen Abmessungen und gos auf 5,7 MB

Hier ist mein Code:

let fotoNSImage = NSImage(byReferencing: url) 

let fotoNSImageRedim = redimensionaNSImage(imagem: fotoNSImage, tamanho: NSSize(width: 200, height: 200)) 

let fotoNSImageRep = NSBitmapImageRep(data: (fotoNSImageRedim.tiffRepresentation)!) 

let fotoNSImagePng = fotoNSImageRep!.representation(using: NSBitmapImageRep.FileType.png, properties: [:]) 

let fotoProduto = FotoProduto(foto: PFFile(data: fotoNSImagePng!)!, categorias: []) 

Das Verfahren de Bild, um die Größe:

static func redimensionaNSImage(imagem: NSImage, tamanho: NSSize) -> NSImage { 

    var ratio: Float = 0.0 
    let imageWidth = Float(imagem.size.width) 
    let imageHeight = Float(imagem.size.height) 
    let maxWidth = Float(tamanho.width) 
    let maxHeight = Float(tamanho.height) 

    if (imageWidth > imageHeight) { 
     // Landscape 
     ratio = maxWidth/imageWidth; 
    } 
    else { 
     // Portrait 
     ratio = maxHeight/imageHeight; 
    } 

    // Calculate new size based on the ratio 
    let newWidth = imageWidth * ratio 
    let newHeight = imageHeight * ratio 

    // Create a new NSSize object with the newly calculated size 
    let newSize: NSSize = NSSize(width: Int(newWidth), height: Int(newHeight)) 

    // Cast the NSImage to a CGImage 
    var imageRect: CGRect = CGRect(x: 0, y: 0, width: Int(newWidth), height: Int(newHeight)) 
    let imageRef = imagem.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) 

    // Create NSImage from the CGImage using the new size 
    let imageWithNewSize = NSImage(cgImage: imageRef!, size: newSize) 

    // Return the new image 
    return imageWithNewSize 
} 

Ich habe die folgenden Ansätze bereits erfolglos versucht.

1 - Ändern Sie den pixelWide und pixelHigh direkt auf fotoNSImagePng:

fotoNSImageRep?.pixelsWide = 200 
fotoNSImageRep?.pixelsHigh = 133 

2 - ein neues NSBitmapImageRep Erstellen und ersetzen Sie die Bilddarstellung mit ihm:

let rep = NSBitmapImageRep(bitmapDataPlanes: nil, 
          pixelsWide: 200, 
          pixelsHigh: 133, 
          bitsPerSample: 8, 
          samplesPerPixel: 4, 
          hasAlpha: true, 
          isPlanar: false, 
          colorSpaceName: NSDeviceRGBColorSpace, 
          bytesPerRow: Int(newSize.width * 4), 
          bitsPerPixel: 32) 

3 - Folgen Sie dieser Ansatz: How to resize - resample - change file size - NSImage

4 - Ändern Sie die NSBitmapImageRep-Größenwerte:

fotoNSImageRep?.size.width = 200 
fotoNSImageRep?.size.height = 133 

Nichts hat bisher funktioniert.

Antwort

0

Ich habe das Verfahren modifiziert, das das Bild ändert die Größe

static func redimensionaNSImage(imagem: NSImage, tamanho: NSSize) -> NSImage { 

    var ratio: Float = 0.0 
    let imageWidth = Float(imagem.size.width) 
    let imageHeight = Float(imagem.size.height) 
    let maxWidth = Float(tamanho.width) 
    let maxHeight = Float(tamanho.height) 

    // Get ratio (landscape or portrait) 
    if (imageWidth > imageHeight) { 
     // Landscape 
     ratio = maxWidth/imageWidth; 
    } 
    else { 
     // Portrait 
     ratio = maxHeight/imageHeight; 
    } 

    // Calculate new size based on the ratio 
    let newWidth = imageWidth * ratio 
    let newHeight = imageHeight * ratio 

    let imageSo = CGImageSourceCreateWithData(imagem.tiffRepresentation! as CFData, nil) 
    let options: [NSString: NSObject] = [ 
     kCGImageSourceThumbnailMaxPixelSize: max(imageWidth, imageHeight) * ratio as NSObject, 
     kCGImageSourceCreateThumbnailFromImageAlways: true as NSObject 
    ] 
    let size1 = NSSize(width: Int(newWidth), height: Int(newHeight)) 
    let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSo!, 0, options as CFDictionary).flatMap { 
     NSImage(cgImage: $0, size: size1) 
    } 

    return scaledImage! 
}