用名称或任何类型的元数据保存照片 [英] Saving a photo with a name, or any kind of metadata

查看:67
本文介绍了用名称或任何类型的元数据保存照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完整的应用程序,可以拍照并将其放入自定义相册中.我可以为每张专辑命名,也可以完美地检索所有图像.但是,我真正需要的是能够命名单个照片(或使用某种元数据),以便我可以在应用程序内的适当时间显示它们.

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.我使用的是Objective-C,而不是SWIFT.

PS. I am using Objective-C not SWIFT.

推荐答案

您可以通过两种方式执行此操作:

You can do this in two ways:

1-将照片保存在临时目录中.示例:

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-使用 Photos Framework 保存.示例:

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!"))
                        })
                })

            })
        }

您可以检查项目示例.

这篇关于用名称或任何类型的元数据保存照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆