Saving image to Documents directory and retrieving for email attachment

前端 未结 4 1454
遇见更好的自我
遇见更好的自我 2020-11-28 04:07

I having trouble figuring out NSBundle & DocumentDirectory data, I have a Camera Picture \"imageView\" that I\'m saving to the NSDocumentDire

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 04:34

    Swift 3

    // Create a URL
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let imageURL = documentsURL?.appendingPathComponent("MyImageName.png")
    
    // save image to URL
    let myImage = imageView.image! // or wherever you have your UIImage
    
    do {
        try UIImagePNGRepresentation(myImage)?.write(to: imageURL!)
    } catch {}
    
    
    // Use the URL to retrieve the image for sharing to email, social media, etc.
    // docController.URL = imageURL
    // ...
    

    I force unwrapped some of the optionals for brevity. Use guard or if let in your code.

提交回复
热议问题