2012-05-26 80 views
2

我正在開發一個應用程序,該應用程序讀取圖像的地理位置並允許用戶修改此信息並將此數據寫回。我使用writeImageDataToSavedPhotosAlbum函數成功讀取數據,操作和寫入庫。問題是,改爲更新原始圖像,它創建一個新的。替換iOS中的ALAsset對象ALAssetsLibrary

我該如何更換或更新該項目?

[...] 

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

[...] 

在此先感謝

回答

7

你只能改變你的應用程序創造的照片(見的ALAsseteditable屬性的文檔)。爲此,請在代表照片的ALAsset上致電setImageData:metadata:completionBlock:

還有一個writeModifiedImageDataToSavedPhotosAlbum:metadata:completionBlock:方法,但它總是創建一個新的資產,被認爲是原始資產的修改版本(我不確定如何使用這些信息)。

+0

因此,假設我使用我的應用程序獲取照片,關閉應用程序,並且在我希望修改元數據後的第二天,那麼可能呢?還是隻是在運行時,當我得到的圖片,然後我把它保存到圖書館? –

+0

這應該是可以的,是的。 – omz

+0

「元數據:」字典參數的格式記錄在哪裏?我似乎無法正確設置它。 –