【问题标题】:Replace ALAsset object in iOS ALAssetsLibrary替换 iOS ALAssetsLibrary 中的 ALAsset 对象
【发布时间】:2012-05-26 17:08:58
【问题描述】:

我正在开发一个应用程序,它可以读取图像的地理位置并允许用户修改此信息并将此数据写回。我使用 writeImageDataToSavedPhotosAlbum 函数成功读取数据、操作和写入库。问题是它不是更新原始图像,而是创建一个新图像。

如何替换或更新该项目?

[...]

NSMutableDictionary *EXIFDictionary = [[[metadata objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]autorelease];
NSMutableDictionary *GPSDictionary  = [[[metadata objectForKey:(NSString *)kCGImagePropertyGPSDictionary]mutableCopy]autorelease];
NSMutableDictionary *TIFFDictionray = [[[metadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary]mutableCopy]autorelease];

Byte *buffer = (Byte*)malloc(representation.size);
NSUInteger buffered = [representation getBytes:buffer fromOffset:0.0 length:representation.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES]; //this is NSData may be what you want

if(!EXIFDictionary) {
    //if the image does not have an EXIF dictionary (not all images do), then create one for us to use
    EXIFDictionary = [NSMutableDictionary dictionary];
}
if(!GPSDictionary) {
    GPSDictionary = [NSMutableDictionary dictionary];
}
if(!TIFFDictionray) {
    TIFFDictionray = [NSMutableDictionary dictionary];
}


[TIFFDictionray setObject:@"This should be the image description" forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
[EXIFDictionary setObject:@"This should be the user comment" forKey:(NSString*)kCGImagePropertyExifUserComment];

[metadataAsMutable setObject:TIFFDictionray forKey:(NSString*)kCGImagePropertyTIFFDictionary];
[metadataAsMutable setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];

__block NSDate *date = [[NSDate date] retain];

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];

[library writeImageDataToSavedPhotosAlbum:imageData metadata:metadataAsMutable completionBlock:^(NSURL *assetURL, NSError *error) {
    NSLog(@"Saving Time: %g", [[NSDate date] timeIntervalSinceDate:date]);
    [date release];
}];

[...]

提前致谢

【问题讨论】:

    标签: ios xcode cocoa alassetslibrary alasset


    【解决方案1】:

    您只能更改您的应用创建的照片(请参阅ALAsseteditable 属性的文档)。为此,请在代表照片的 ALAsset 上调用 setImageData:metadata:completionBlock:

    还有一个writeModifiedImageDataToSavedPhotosAlbum:metadata:completionBlock: 方法,但它总是创建一个新资产,该资产被认为是原始资产的修改版本(我不确定如何使用该信息)。

    【讨论】:

    • 所以,假设我使用我的应用程序获得了一张照片,我关闭了应用程序,并且在我想修改元数据的第二天,那么可能吗?还是只是在运行时,当我得到图片并将其保存到库之前?
    • 应该可以,是的。
    • metadata: 字典参数的格式记录在哪里?我似乎无法正确设置它。
    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2015-01-01
    • 2019-09-17
    相关资源
    最近更新 更多