UIImage对图像数据进行解码,以便编辑和显示,所以
UIImageWriteToSavedPhotosAlbum([UIImage imageWithContentsOfFile:[NSData dataWithContentsOfFile:[self getImagePath]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
首先将图像解码,然后通过UIImageWriteToSavedPhotosAlbum 方法将其编码回来。
相反,您应该使用ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock:,如下所示:
ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLib writeImageDataToSavedPhotosAlbum:[self getImagePath] metadata:nil completionBlock:nil];
您还可以将元数据和完成块传递给调用。
编辑:
获取图片:
[info objectForKey:@"UIImagePickerControllerOriginalImage"] 包含从UIImagePickerController 中选择的解码UIImage。你应该改用
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
使用assetURL,您现在可以使用ALAssetsLibrary/assetForURL:resultBlock:failureBlock: 方法为其获取ALAsset:
ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLib assetForURL:assetURL resultBlock:resultBlock failureBlock:failureBlock];
您现在可以获取该图像的未更改的 NSData:
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset){
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
long long imageDataSize = [assetRepresentation size];
uint8_t* imageDataBytes = malloc(imageDataSize);
[assetRepresentation getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES]; // you could for instance read data in smaller buffers and append them to your file instead of reading it all at once
// save it
[imgdata writeToFile:filePath atomically:NO];
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
NSLog(@"Cannot get image - %@",[myerror localizedDescription]);
//
};
我可能在代码中犯了一些错误,但步骤如上所列。如果某些事情不能正常工作,或者如果您想提高一点效率,有很多例子可以在 stackoverflow 或其他网站上从 ALAsset 中读取 NSData。