NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];
void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset) {
if (asset != nil) {
[[NSFileManager defaultManager] removeItemAtPath:[PaiKeUnity getPrevImgPath] error:nil];
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *imageBuffer = (Byte*)malloc(rep.size);
NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
[imageData writeToFile:[PaiKeUnity getPrevImgPath] atomically:YES];
[self setPrevImage];
}
else {
}
};
[assetLibrary assetForURL:imageRefURL
resultBlock:ALAssetsLibraryAssetForURLResultBlock
failureBlock:^(NSError *error){
}];
[assetLibrary release];
ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];
void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset) {
if (asset != nil) {
[[NSFileManager defaultManager] removeItemAtPath:[PaiKeUnity getPrevImgPath] error:nil];
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *imageBuffer = (Byte*)malloc(rep.size);
NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
[imageData writeToFile:[PaiKeUnity getPrevImgPath] atomically:YES];
[self setPrevImage];
}
else {
}
};
[assetLibrary assetForURL:imageRefURL
resultBlock:ALAssetsLibraryAssetForURLResultBlock
failureBlock:^(NSError *error){
}];
[assetLibrary release];
本文介绍了一种在iOS应用中从相册选择图片并进行处理的方法。通过使用ALAssetsLibrary和UIImagePickerController,实现了获取选中图片的URL,进一步加载ALAsset并获取其默认表示形式以读取图像数据,最后保存到指定路径。
2796

被折叠的 条评论
为什么被折叠?



