2016-08-30 59 views
0

我正在尋找上傳圖像文件,調整其大小(以縮小文件總大小)的方法,然後將其另存爲PNG。我認爲這樣做肯定有一個相當直接的方法,但幾個小時的搜索沒有取得有效的結果。我已經能夠通過以壓縮的JPEG格式導出圖像來實現所需的文件大小,但我需要保持透明度。下面是我用來獲取JPEG代碼:OSX - 使用Swift加載圖像並將其保存爲較小的PNG文件

func chooseImage() { 
    var image = NSImage() 

    //choose image from hard disk 
    let panel = NSOpenPanel() 
    panel.allowsMultipleSelection = false 
    panel.canChooseFiles = true 
    panel.canChooseDirectories = false 
    panel.runModal() 
    panel.allowedFileTypes = ["png", "jpeg", "jpg"] 

    let chosenFile = panel.URL 

    //convert to NSData and send to Jpeg function 
    if chosenFile != nil { 
     image = NSImage(contentsOfURL: chosenFile!)! 
     let imageData = image.TIFFRepresentation 
     self.saveAsJpeg(imageData!, compression: 0.5) 
    } 
} 




func saveAsJpeg (image:NSData, compression:NSNumber) { 
    // make imagerep and define properties 
    let imgRep = NSBitmapImageRep(data: image) 
    let props = NSDictionary.init(object: compression, forKey: NSImageCompressionFactor) 
    let pingy = imgRep?.representationUsingType(NSBitmapImageFileType.NSJPEGFileType, properties: props as! [String : AnyObject]) 

    //save to disk 
    let documentURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! 
    let folderURL = documentURL.URLByAppendingPathComponent("KKNightlife Data") 

    let g = GetUniqueID() 
    let fileName = g.getUniqueID() + ".jpeg" 

    do { 
     try NSFileManager.defaultManager().createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil) 
    } catch { 
     print("cannot create directory - folder Exists?") 
    } 
    let url = folderURL.URLByAppendingPathComponent(fileName) 

    if let pid = pingy { 
     pid.writeToURL(url, atomically: false) 
    } else { 
     print("error saving image") 
    } 
} 

我嘗試使用下面的代碼來縮放圖像,以創建一個較小的PNG文件,但是不管我的尺寸輸入什麼樣的價值觀,生成的文件是相同的大小(二者在高度/寬度和整個文件的大小而言):

func chooseImage (size:String) { 
    let panel = NSOpenPanel() 
    panel.allowsMultipleSelection = false 
    panel.canChooseFiles = true 
    panel.canChooseDirectories = false 
    panel.runModal() 
    panel.allowedFileTypes = ["png", "jpeg", "jpg"] 

    let chosenFile = panel.URL 

    if chosenFile != nil { 
     let image = NSImage(contentsOfURL: chosenFile!) 
     self.scaleImage(image!) 
    } 
} 


func scaleImage (image:NSImage) { 

    //create resized image 
    let newSize = NSSize(width: 10, height: 10) 
    var imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height) 
    let imageRef = image.CGImageForProposedRect(&imageRect, context: nil, hints: nil) 
    let resizedImage = NSImage(CGImage: imageRef!, size: newSize) 

    let imageData = resizedImage.TIFFRepresentation 

    //make imagerep 
    let imgRep = NSBitmapImageRep(data: imageData!) 
    let pingy = imgRep?.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [:]) 

    //save to disk 
    let documentURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! 
    let g = GetUniqueID() 
    let fileName = g.getUniqueID() + ".png" 

    let folderURL = documentURL.URLByAppendingPathComponent("KKNightlife Data") 
    do { 
     try NSFileManager.defaultManager().createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil) 
    } catch { 
     print("cannot create directory - folder Exists?") 
    } 
    let url = folderURL.URLByAppendingPathComponent(fileName) 
    if let pid = pingy { 
     pid.writeToURL(url, atomically: false) 
     print("image is at \(documentURL)") 
    } else { 
     print("error saving image") 
    } 
} 

任何建議,將不勝感激。

回答

0

我終於能夠使用這個發現這裏的擴展做: https://gist.github.com/raphaelhanneken/cb924aa280f4b9dbb480

這是我怎麼會叫他們萬一有人遇到類似的問題:

func chooseImage (size:String) { 
    let panel = NSOpenPanel() 
    panel.allowsMultipleSelection = false 
    panel.canChooseFiles = true 
    panel.canChooseDirectories = false 
    panel.runModal() 
    panel.allowedFileTypes = ["png", "jpeg", "jpg"] 

    let chosenFile = panel.URL 

    if chosenFile != nil { 
     let image = NSImage(contentsOfURL: chosenFile!) 
     self.scaleImageUsingExtensions(image!) 
    } 
} 

func scaleImageUsingExtensions (image:NSImage){ 
    let size: NSSize = NSMakeSize(10, 10) 
    let resizedImage = image.resizeWhileMaintainingAspectRatioToSize(size) 
    let documentURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! 
    let g = GetUniqueID() 
    let fileName = g.getUniqueID() + ".png" 

    let folderURL = documentURL.URLByAppendingPathComponent("KKNightlife Data") 
    do { 
     try NSFileManager.defaultManager().createDirectoryAtURL(folderURL, withIntermediateDirectories: false, attributes: nil) 
    } catch { 
     print("cannot create directory - folder Exists?") 
    } 
    let url = folderURL.URLByAppendingPathComponent(fileName) 
    do { 

    try resizedImage?.savePNGRepresentationToURL(url) 

    } 
    catch { 
     print("error saving file") 
    } 
} 
相關問題