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

纵饮孤独 提交于 2019-12-11 06:38:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32014235/saving-a-photo-with-a-name-or-any-kind-of-metadata

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