替换iOS ALAssetsLibrary中的ALAsset对象 [英] Replace ALAsset object in iOS ALAssetsLibrary
问题描述
我正在开发一个应用程序,它读取图像的地理位置,并允许用户修改此信息并将此数据写回。我成功读取数据,通过使用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]; //这是NSData可能是你想要的
if(!EXIFDictionary){
//如果图像没有EXIF字典(不是所有的图像),然后创建一个我们使用
EXIFDictionary = [NSMutableDictionary dictionary];
}
if(!GPSDictionary){
GPSDictionary = [NSMutableDictionary dictionary];
}
if(!TIFFDictionray){
TIFFDictionray = [NSMutableDictionary dictionary];
}
[TIFFDictionray setObject:@这应该是图像描述forKey:(NSString *)kCGImagePropertyTIFFImageDescription];
[EXIFDictionary setObject:@这应该是用户注释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 release];
}];
[...]
提前感谢
您只能更改应用程式已建立的相片(请参阅可编辑
属性 ALAsset
)。为此,请在代表照片的 ALAsset
上调用 setImageData:metadata:completionBlock:
b
$ b
还有一个 writeModifiedImageDataToSavedPhotosAlbum:metadata:completionBlock:
方法,但它总是创建一个新的资产,资产(我不确定如何使用这些信息)。
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
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).
这篇关于替换iOS ALAssetsLibrary中的ALAsset对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!