- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在更新图像的元数据并将其保存回照片库时遇到问题。一切工作除了图像元数据在它被改变之后缺少之前存在的条目并且我在处理图像或执行照片库更改 block 时没有收到任何错误。此外,在将字典写回图像之前,字典看起来就像是原始字典加上我在调试器中的字典。
我的问题是:
编辑:
在保存之前,所有 Exif 和 Tiff 值都存在。这是使用以下代码保存到照片后的全部元数据:
["PixelHeight": 2448, "PixelWidth": 3264, "{Exif}": {
ColorSpace = 1;
PixelXDimension = 3264;
PixelYDimension = 2448;}, "Depth": 8, "ProfileName": sRGB IEC61966-2.1, "Orientation": 1, "{TIFF}": {
Orientation = 1;}, "ColorModel": RGB, "{JFIF}": {
DensityUnit = 0;
JFIFVersion = (
1,
0,
1
);
XDensity = 72;
YDensity = 72;}]
代码,全部使用 Swift 3,在 iOS 10.1 上测试
基本工作流程是:
// Get a mutable copy of the existing Exif meta
let mutableMetaData = getMutableMetadataFrom(imageData: data)
// Check to see if it has the {GPS} entry, if it does just exit.
if let _ = mutableMetaData[kCGImagePropertyGPSDictionary as String] {
callback(imageAsset, true, nil)
return
}
// Add the {GPS} tag to the existing metadata
let clLocation = media.location!.asCLLocation()
mutableMetaData[kCGImagePropertyGPSDictionary as String] =
clLocation.asGPSMetaData()
// Attach the new metadata to the existing image
guard let newImageData = attach(metadata: mutableMetaData, toImageData: data) else {
callback(imageAsset, false, nil)
return
}
let editingOptions = PHContentEditingInputRequestOptions()
imageAsset.requestContentEditingInput(with: editingOptions) { editingInput, info in
guard let editingInput = editingInput else { return }
let library = PHPhotoLibrary.shared()
let output = PHContentEditingOutput(contentEditingInput: editingInput)
output.adjustmentData = PHAdjustmentData(formatIdentifier: "Project", formatVersion: "0.1",
data: "Location Adjustment".data(using: .utf8)!)
do {
try newImageData.write(to: output.renderedContentURL, options: [.atomic])
} catch {
callback(imageAsset, false, error)
return
}
library.performChanges({
let changeRequest = PHAssetChangeRequest(for: imageAsset)
changeRequest.location = clLocation
changeRequest.contentEditingOutput = output
}, completionHandler: { success, error in ... ...
工作流的辅助方法是:
func attach(metadata: NSDictionary, toImageData imageData:Data) -> Data? {
guard
let imageDataProvider = CGDataProvider(data: imageData as CFData),
let cgImage = CGImage(jpegDataProviderSource: imageDataProvider, decode: nil,
shouldInterpolate: true, intent: .defaultIntent),
let newImageData = CFDataCreateMutable(nil, 0),
let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
"image/jpg" as CFString, kUTTypeImage),
let destination = CGImageDestinationCreateWithData(newImageData,
(type.takeRetainedValue()), 1, nil) else {
return nil
}
CGImageDestinationAddImage(destination, cgImage, metadata as CFDictionary)
CGImageDestinationFinalize(destination)
guard
let newProvider = CGDataProvider(data: newImageData),
let newCGImage = CGImage(jpegDataProviderSource: newProvider, decode: nil,
shouldInterpolate: false, intent: .defaultIntent) else {
return nil
}
return UIImageJPEGRepresentation(UIImage(cgImage: newCGImage), 1.0)
}
func getMutableMetadataFrom(imageData data : Data) -> NSMutableDictionary {
let imageSourceRef = CGImageSourceCreateWithData(data as CFData, nil)
let currentProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef!, 0, nil)
let mutableDict = NSMutableDictionary(dictionary: currentProperties!)
return mutableDict
}
此外,asGPSMetaData
是 CLLocation
的扩展,而不是 this Gist 的 Swift 3 版本。
最佳答案
事实证明,问题根本不是使用 CoreGraphics 处理图像或元数据,而是我忽略的几件事:
PHAssetChangeRequest::creationRequestForAssetFromImage(atFileURL:)
或使用 PHAssetCreationRequest::forAsset
创建创建请求然后使用 PHAssetCreationRequest::addResource(with:data:options:)
将数据添加为照片。我选择了后者,因为它移动较少部分。所以我想所有这些都取代了漂亮、简洁的 ALAssetsLibrary::writeImage(toSavedPhotosAlbum:metadata:completionBlock:)
。
照片库的最终更改 block 最终是这样的:
var assetID: String?
PHPhotoLibrary.shared().performChanges({
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: .photo, data: imageWithMetaData as Data, options: nil)
creationRequest.location = clLocation
assetID = creationRequest.placeholderForCreatedAsset?.localIdentifier
}) { success, error in ...
关于ios - 将更新的图像保存到 PhotoKit 时缺少图像元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41169156/
我是 iOS 8 的新手,我想使用 photoKit 编辑输入图像,例如模糊、旋转、颜色变化、创建渐变等。我已经搜索过,没有教程,所以请分享详细的想法。提前致谢。 最佳答案 浏览解释照片框架的 WWD
有什么方法可以保留用户在“照片”应用中排列所有相册的列表顺序?我一整天都在尝试解决这个问题,但我似乎无法弄清楚我缺少什么来保留在我的应用程序中显示的顺序。 我想保留顺序,因为用户可以在照片应用中重新排
我第一次开始尝试 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。正如您在此屏幕截图中看到的,我定义了 18 个智能相册和 1 个常规相册。 但是,当我获取智能相册
我第一次开始尝试 macOS 开发。我正在尝试查询我在 macOS 照片应用程序中创建的智能相册列表。正如您在此屏幕截图中看到的,我定义了 18 个智能相册和 1 个常规相册。 但是,当我获取智能相册
我正在使用 PhotoKit 来获取其中一个系统相册中的照片,如下所示: PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; f
我了解如何使用 Apple 的 PhotoKit API 以编程方式创建文件夹并将照片添加到该文件夹。 PHPhotoLibrary.shared().performChanges({
我正在使用 iOS 8 和新的 PhotoKit 开发一个 iOS 应用程序,我遍历用户图像并试图通过代码找出图像是垂直的还是水平的? Assets 上有我可以检查的标志吗? 最佳答案 您可以使用 r
我目前正在使用 ALAsset 框架将图像从照片库保存到带有元数据的文档目录中。我使用的代码是 ALAssetsLibrary *library = [[[ALAssetsLibrary alloc]
我可以使用 PHFetchResult *results = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil]; 获取在设备
我正在使用 PhotoKit 从系统相册中获取照片并将它们放入 UICollectionView 中。 对于 UICollectionViewCell,我这样设置: cell.imageView.co
以下代码通过 3 个简单步骤突出显示了该问题: 1) 获取时刻 2)缓存时刻localIdentifier 3) 使用标识符获取时刻:失败(在设备上,iOS 8.2) - ( void )moment
我在更新图像的元数据并将其保存回照片库时遇到问题。一切工作除了图像元数据在它被改变之后缺少之前存在的条目并且我在处理图像或执行照片库更改 block 时没有收到任何错误。此外,在将字典写回图像之前,字
我正在尝试使用照片框架重现原生 iOS8 的照片选择器。我在排序时遇到了问题。 假设我执行以下操作: 我在 Camera+ 应用程序中编辑照片并将其保存回图库。 我收藏了另一张照片。 在 native
当我打印 PHAsset 的描述时,它包括 assetSource=3 或 assetSource=0。 我可以知道这是什么吗assetSource 是什么意思?像 3,0 这样的数字与什么有关?根据
我想从视频资源中提取 UIimage,用作海报。根据 PHImageManager 的文档,我应该能够使用 requestImageForAsset:targetSize:contentMode:op
我使用方法(这个问题的结尾)从设备中检索视频。它的作用是在库中找到第一个视频,创建导出 session 并将视频导出到 MOV 文件中。 应用程序运行两次后(在方法运行之间停止应用程序),将比较两个生
我正在开发 iOS 8 应用程序并处理大量照片,我需要唯一标识 PHassets 但找不到任何可用的属性?我找到一处特性在 PHObject/localIdentifier 记录在这里 https:
我正在使用下面的代码来获取图像: [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(8
我在理解如何为 photokit 查询制定文件管理器时遇到了很多麻烦,特别是当您只想检索某些媒体类型时如何制定 NSPredicate。就我而言,我正在尝试检索所有相册(即 PHCollectionA
我是否可以使用从 "PHImageFileURLKey" 返回的路径进入照片库并检索图像? 返回的路径格式如下: "file:///var/mobile/Media/DCIM/102APPLE/IMG
我是一名优秀的程序员,十分优秀!