MarioAna
MarioAna

Reputation: 865

Saving a photo with a name, or any kind of metadata

I've got a completed app that takes photos and puts them in custom albums. I can name each album and I can retrieve all the images perfectly. However what I really need is to be able to name the individual photos (or use some kind of metadata) so that I can show them at appropriate times inside the app.

I know it can be done if you are storing the photos in the app's documents directory but I've had to move away from that and go with the device's photo library.

Has anyone got any ideas around how to do this?

PS. I am using Objective-C not SWIFT.

Upvotes: 1

Views: 765

Answers (1)

mcvasquez
mcvasquez

Reputation: 36

You can do this in two ways:

1- Saving the photo in a temporary directory. Example:

var fileManager = NSFileManager()
var tmpDir = NSTemporaryDirectory()
let filename = "YourImageName.png"
let path = tmpDir.stringByAppendingPathComponent(filename)
var error: NSError?
let imageData = UIImagePNGRepresentation(YourImageView.image)
fileManager.removeItemAtPath(path, error: nil)
println(NSURL(fileURLWithPath: path))

if(imageData.writeToFile(path,atomically: true)){
    println("Image saved.")
}else{
    println("Image not saved.")
}

2- Saving using Photos Framework. Example:

if let image: UIImage = YourImageView.image

            let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
            dispatch_async(dispatch_get_global_queue(priority, 0), {
                PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                    let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
                    let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset
                    if let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) {
                        albumChangeRequest.addAssets([assetPlaceholder])
                    }
                    }, completionHandler: {(success, error)in
                        dispatch_async(dispatch_get_main_queue(), {
                            NSLog("Adding Image to Library -> %@", (success ? "Sucess":"Error!"))
                        })
                })

            })
        }

You can check this project sample.

Upvotes: 1

Related Questions