14

After i took a photo with the camera i save the image with the following code:

UIImageWriteToSavedPhotosAlbum(image, self,"image:didFinishSavingWithError:contextInfo:", nil)

This saves the image in the camera roll. Additionally i want to save the URL to this saved image in Core Data. Is it possible to retrieve the URL (or the Path) to so that i only need to save this URL and not the full image in Core Data?

ManfredP
  • 1,037
  • 1
  • 12
  • 27
  • Possible duplicate of: http://stackoverflow.com/questions/4457904/iphone-how-do-i-get-the-file-path-of-an-image-saved-with-uiimagewritetosavedpho – Juul Mar 12 '15 at 12:52
  • 1
    This answers are unfortunately written in ObjectiveC. I would need this in Swift Programming language ... – ManfredP Mar 12 '15 at 13:56

2 Answers2

9

After reading a lot of answers written in ObjectivC i finally figured out how to write this in Swift with Xcode 6.2:

ALAssetsLibrary().writeImageToSavedPhotosAlbum(image.CGImage, orientation: ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!,
            completionBlock:{ (path:NSURL!, error:NSError!) -> Void in
                println("\(path)")
        })
ManfredP
  • 1,037
  • 1
  • 12
  • 27
  • 1
    Don't forget the import statement: `import AssetsLibrary` ;) – gfpacheco Jul 22 '15 at 14:45
  • 3
    N.B. This API is deprecated as of iOS 9. As Tys said in a comment in http://stackoverflow.com/a/4458451/171144 "Instead use the PHPhotoLibrary and the "performChanges" method to save images to the device." – fractious Jul 05 '16 at 11:07
2

Here is my combination..

Saving Image :

    let imageData = NSData(data:UIImagePNGRepresentation(pickedimage))
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    var docs: String = paths[0] as! String
    let fullPath = docs.stringByAppendingPathComponent("yourNameImg.png")
    let result = imageData.writeToFile(fullPath, atomically: true)

    print(fullPath)

Get Image:

    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("yourNameImg.png")
                let image    = UIImage(contentsOfFile: readPath)

                UploadImagePreview.image = image
            }
        }
    }

We can try with Photos Framework to fetch the last saved image.

    var fetchOptions: PHFetchOptions = PHFetchOptions()

    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

    var fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)

    if (fetchResult.firstObject != nil) {

        var lastAsset: PHAsset = fetchResult.lastObject as! PHAsset

        PHImageManager.defaultManager().requestImageForAsset(lastAsset, targetSize: self.UploadImagePreview.bounds.size, contentMode: PHImageContentMode.AspectFill, options: PHImageRequestOptions(), resultHandler: { (result, info) -> Void in

            self.UploadImagePreview.image = result

        })

    }
Alvin George
  • 14,148
  • 92
  • 64