gpt4 book ai didi

ios - 在 iOS 上使用修改后的元数据(无重新编码)保存原始图像数据

转载 作者:技术小花猫 更新时间:2023-10-29 10:23:52 32 4
gpt4 key购买 nike

我想在临时文件夹中保存一些元数据更改的图像,而不重新编码实际图像数据。

我发现能够做到这一点的唯一方法是 ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock: , 但是,这个将图像保存到照片库。相反,我想将图像保存到一个临时文件夹(例如通过电子邮件共享它,而不填充照片库)。

我试过使用 CGImageDestinationRef ( CGImageDestinationAddImageFromSource ),但它只能使用解码图像创建,这意味着它在保存时重新编码它(经过测试,像素字节看起来不同) .

除了使用 CGImageDestinationRef 之外,iOS 是否还有其他方法/类可以将图像数据与元数据一起保存?也欢迎提出解决方法的建议。

最佳答案

这是 iOS SDK 的一个令人沮丧的问题。首先,我建议提交 enhancement request .

现在,这是一个潜在的解决方法:如果 ALAsset 是由您创建的(即,它的 editable 属性是 YES),那么您基本上可以读取数据,写入元数据,再次读取,保存到磁盘,然后用原始元数据写入。

这种方法将避免创建重复图像。

请阅读//评论,因为我为简洁起见跳过了一些内容(例如构建元数据字典):

ALAsset* asset; //get your asset, don't use this empty one
if (asset.editable) {

// get the source data
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
// add error checking here
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *sourceData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

// make your metadata whatever you want
// you should use actual metadata, not a blank dictionary
NSDictionary *metadataDictionary = [NSDictionary dictionary];

// these are __weak to avoid creating an ARC retain cycle
NSData __weak *originalData = sourceData;
NSDictionary __weak *originalMetadata = [rep metadata];

[asset setImageData:sourceData
metadata:metadataDictionary
completionBlock:^(NSURL *assetURL, NSError *error) {
//now get your data and write it to file
if (!error) {
//get your data...
NSString *assetPath = [assetURL path];
NSData *targetData = [[NSFileManager defaultManager] contentsAtPath:assetPath];

//...write to file...
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths lastObject];
NSURL *fileURL = [NSURL fileURLWithPath:documentPath];
[targetData writeToURL:fileURL atomically:YES];

//...and put it back the way it was
[asset setImageData:originalData metadata:originalMetadata completionBlock:nil];
} else {
// handle error on setting data
NSLog(@"ERROR: %@", [error localizedDescription]);
}
}];
} else {
// you'll need to make a new ALAsset which you have permission to edit and then try again

}

如您所见,如果 ALAsset 不归您所有,您将需要创建一个,这会将一张照片添加到用户的图库中,这正是您想要避免的。但是,正如您可能已经猜到的那样,您不能从用户的照片库中删除 ALAsset,即使您的应用程序创建了它。 (请随时为此提交另一个增强请求。)

因此,如果照片/图像是在您的应用中创建的,那么这将适用于您。

但如果没有,它将创建一个用户必须删除的额外副本。

唯一的选择是自己解析 NSData,这会很痛苦。据我所知,没有任何开源库可以填补 iOS SDK 中的这一空白。

关于ios - 在 iOS 上使用修改后的元数据(无重新编码)保存原始图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14624384/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com