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"]
UIImage
包含从中选择的解码UIImagePickerController
。你应该改用
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
使用assetURL
您现在可以ALAsset
使用ALAssetsLibrary/assetForURL:resultBlock:failureBlock:方法获取它:
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]);
//
};
我可能在代码中犯了一些错误,但步骤如上所列。如果某些事情不能正常工作,或者如果你想让它更有效率,有很多例子可以做一些事情,比如NSData
从ALAsset
stackoverflow 或其他网站上阅读。