Replace ALAsset object in iOS ALAssetsLibrary

此生再无相见时 提交于 2019-12-24 02:37:26

问题


I am developing an application that reads the geo location of an image and allows the user to modify this information and write this data back. I successfully read the data, manipulate and write to the library by using writeImageDataToSavedPhotosAlbum function. The problem is that instead updating the original image, it creates a new one.

How could i replace or update that item ?

[...]

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];
}];

[...]

Thanks in advance


回答1:


You can only change photos that your app has created (see the documentation on the editable property of ALAsset). To do so, call setImageData:metadata:completionBlock: on the ALAsset that represents the photo.

There's also a writeModifiedImageDataToSavedPhotosAlbum:metadata:completionBlock: method, but it always creates a new asset that is considered a modified version of the original asset (I'm not exactly sure how that information is used).



来源:https://stackoverflow.com/questions/10768257/replace-alasset-object-in-ios-alassetslibrary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!