问题
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