简体   繁体   English

使用日期时间元数据将图像保存到相册

[英]Save image to photo album with datetime metadata

I'm trying to use the metadata property in writeImageDataToSavedPhotosAlbum to save a GIF to the iPhone's photo album and add date/time metadata so that the saved GIF appears in a different place in the user's photo album instead of the default location at the end of the photo album.我正在尝试使用writeImageDataToSavedPhotosAlbum的元数据属性将 GIF 保存到 iPhone 的相册并添加日期/时间元数据,以便保存的 GIF 出现在用户相册中的不同位置,而不是末尾的默认位置相册。

I've tried doing the following:我试过做以下事情:

let metadata: [String: AnyObject]! = [kCGImagePropertyTIFFDictionary as String: 
    [kCGImagePropertyTIFFDateTime as String: dateTime!], 
    kCGImagePropertyExifDictionary as String: 
    [kCGImagePropertyExifDateTimeDigitized as String: dateTime!, 
    kCGImagePropertyExifDateTimeOriginal as String: dateTime!]]
library.writeImageDataToSavedPhotosAlbum(data, metadata: metadata, completionBlock: completionBlock)

and the debug print for the metadata variable shows:元数据变量的调试打印显示:

["{TIFF}": {
    DateTime = "2015:10:09 20:07:48";
}, "{Exif}": {
    DateTimeDigitized = "2015:10:09 20:07:48";
    DateTimeOriginal = "2015:10:09 20:07:48";
}]

However, it doesn't seem like setting the metadata worked because the GIF is still saved at the end of the photo album with the current timestamp instead of the timestamp that I'm trying to set.但是,设置元数据似乎不起作用,因为 GIF 仍然使用当前时间戳而不是我尝试设置的时间戳保存在相册的末尾。

It appears the Photos app does not sort based on the date in the image's metadata (most likely because that metadata can be stored on iCloud, it's potentially not available locally).看起来照片应用程序没有根据图像元数据中的日期进行排序(很可能是因为该元数据可以存储在 iCloud 上,它可能在本地不可用)。 Instead it appears to sort by the creationDate , which is a property defined on PHAsset .相反,它似乎按creationDate排序,这是在PHAsset定义的属性。 You can change that using the Photos framework.您可以使用照片框架更改它。 Something like this should do the trick:像这样的事情应该可以解决问题:

PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
    let request = PHAssetChangeRequest(forAsset: asset)
    request.creationDate = dateTime!
}, completionHandler: { (success: Bool, error: NSError?) -> Void in
    dispatch_async(dispatch_get_main_queue()) {
        //done
    }
})

However, note that the Camera Roll or All Photos album does not sort them the same as the Years, Collections, and Moments view.但是,请注意,“相机胶卷”或“所有照片”相册的排序方式与“年份”、“收藏夹”和“时刻”视图不同。 For some reason Camera Roll/All Photos does not sort on the creationDate while Years, Collections, and Moments does.出于某种原因,相机胶卷/所有照片不会按creationDate排序,而年份、收藏和时刻会排序。 It doesn't use the photo metadata either, so I am not sure what it is examining to sort the photos for that album.它也不使用照片元数据,所以我不确定它正在检查该相册的照片排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM