How to convert code objective c to Swift to save image?

丶灬走出姿态 提交于 2019-12-19 05:51:34

问题


I have seen this code in other post, for save pictures:

  // Create path.
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,      NSUserDomainMask, YES);
 NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];

// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

An d I'm trying convert to swift for save a picture take with avfoundatioin but I dont know type NSDocumentDirectory and NSUserDomainMask here How can convert this??

Thanks!!


回答1:


As follows:

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
    if paths.count > 0 {
        if let dirPath = paths[0] as? String {
            let readPath = dirPath.stringByAppendingPathComponent("Image.png")
            let image = UIImage(named: readPath)
            let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
            UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
        }
    }
}

"paths" is an AnyObject[], so you have to check that its elements can be converted to String.

Naturally, you wouldn't actually use "NSDocumentDirectory" as the name, I just did it for clarity.

Update for Xcode 7.2

NSSearchPathForDirectoriesInDomains now returns [String] rather than [AnyObject]? so use

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
    // ...
}

The fact that .stringByAppendingPathComponent is also deprecated is dealt with in this answer...




回答2:


Here is what I use:

    let fileManager = NSFileManager.defaultManager()
    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
    let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
    let documentsURL = fileManager.URLsForDirectory(nsDocumentDirectory, inDomains: nsUserDomainMask).last



回答3:


This is just a rework of @पवन answer working for Swift 3

import Foundation
import UIKit
import ImageIO
import MobileCoreServices

extension UIImage
{
    func write(at path:String) -> Bool
    {
        let result = CGImageWriteToFile(image: self.cgImage!, filePath: path)
        return result
    }

    private func CGImageWriteToFile(image:CGImage, filePath:String) -> Bool
    {
        let imageURL:CFURL = NSURL(fileURLWithPath: filePath)
        var destination:CGImageDestination? = nil

        let ext = (filePath as NSString).pathExtension.lowercased()
        if ext == "jpg" || ext == "jpeg"
        {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG, 1, nil)
        }
        else if ext == "png" || ext == "pngf"
        {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePNG, 1, nil)
        }
        else if ext == "tiff" || ext == "tif"
        {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeTIFF, 1, nil)
        }
        else  if ext == "bmpf" || ext == "bmp"
        {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeBMP, 1, nil)
        }


        guard destination != nil else {
            fatalError("Did not find any matching path extension to store the image")
        }

        CGImageDestinationAddImage(destination!, image, nil)

        if CGImageDestinationFinalize(destination!)
        {
            return true
        }
        return false
    }
}

And the usage

let pickedImage = UIImage()
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString).appendingPathComponent("image.tif")
if pickedImage.write(at: path) == false
{
    print("failed to write file to disk!")
}



回答4:


Use this category to save any image on document directory

import Foundation
import UIKit
import ImageIO
import MobileCoreServices

extension UIImage {
    func writeAtPath(path:String) -> Bool {

        let result = CGImageWriteToFile(self.CGImage!, filePath: path)
        return result
    }

    private func CGImageWriteToFile(image:CGImageRef, filePath:String) -> Bool {
        let imageURL:CFURLRef = NSURL(fileURLWithPath: filePath)
        var destination:CGImageDestinationRef? = nil

        let ext = (filePath as NSString).pathExtension.uppercaseString

        if ext == "JPG" || ext == "JPEG" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG, 1, nil)
        } else if ext == "PNG" || ext == "PNGF" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePNG, 1, nil)
        } else if ext == "TIFF" || ext == "TIF" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeTIFF, 1, nil)
        } else if ext == "GIFF" || ext == "GIF" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeGIF, 1, nil)
        } else if ext == "PICT" || ext == "PIC" || ext == "PCT" || ext == "X-PICT" || ext == "X-MACPICT" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypePICT, 1, nil)
        } else if ext == "JP2" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeJPEG2000, 1, nil)
        } else  if ext == "QTIF" || ext == "QIF" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeQuickTimeImage, 1, nil)
        } else  if ext == "ICNS" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeAppleICNS, 1, nil)
        } else  if ext == "BMPF" || ext == "BMP" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeBMP, 1, nil)
        } else  if ext == "ICO" {
            destination = CGImageDestinationCreateWithURL(imageURL, kUTTypeICO, 1, nil)
        } else {
            fatalError("Did not find any matching path extension to store the image")
        }

        if (destination == nil) {
            fatalError("Did not find any matching path extension to store the image")
            return false
        } else {
            CGImageDestinationAddImage(destination!, image, nil)

            if CGImageDestinationFinalize(destination!) {
                return false
            }
            return true
        }
    }
}

// This is how to use this category in your application.

func testImageWrite() -> Bool {

    let img = UIImage(named: "test")

    var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
    path = (path as NSString).stringByAppendingPathComponent("Test.png")

    let result = img?.writeAtPath(path)

    return result!
}



回答5:


For xCode 8.1, swift 3.0 you can do:

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!

I think this is a lot cleaner and shorter.



来源:https://stackoverflow.com/questions/24301424/how-to-convert-code-objective-c-to-swift-to-save-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!