UIImage
解码图像数据,以便可以编辑和显示,因此
UIImageWriteToSavedPhotosAlbum([UIImage imageWithContentsOfFile:[NSData dataWithContentsOfFile:[self getImagePath]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
首先对图像进行解码,然后通过UIImageWriteToSavedPhotosAlbum
method.
相反,你应该使用ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:元数据:completionBlock:,像这样:
ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];
[assetLib writeImageDataToSavedPhotosAlbum:[self getImagePath] metadata:nil completionBlock:nil];
您还可以将元数据和完成块传递给调用。
EDIT:
为了获取图像:
[info objectForKey:@"UIImagePickerControllerOriginalImage"]
包含解码后的UIImage
选自UIImagePickerController
。你应该使用
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
使用assetURL
你现在可以得到ALAsset
对于它使用ALAssetsLibrary/assetForURL:resultBlock:failureBlock: method:
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 或其他网站上。