iOS開發技巧之:獲取ios相簿gif圖片 原資料
阿新 • • 發佈:2018-11-10
<AssetsLibrary/AssetsLibrary.h>
從Safari上儲存了一張動態GIF到本地的相簿中
可以確定,儲存到本地相簿的動態GIF沒有問題,只是iPhone的相簿不能顯示動態GIF
然後在自己的應用中,要可以選擇GIF圖片上傳到伺服器
用UIImagePickerController得到的資訊是如下的格式
UIImagePickerControllerCropRect = "NSRect: {{0, 0}, {640, 480}}";
UIImagePickerControllerEditedImage = "<UIImage: 0x1e0f0080>";
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "<UIImage: 0x1e0ea110>";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.GIF?id=B6703C91-CD81-4940-A5B0-D3B649FDD015&ext=GIF";
UIImagePickerControllerReferenceURL這個屬性的路徑沒用辦法使用,用這個路徑得到的NSData是nil
UIImagePickerControllerOriginalImage這個屬性雖然能得到UIImage
但是UIImage轉NSData的方法,只有UIImagePNGRepresentation和UIImageJPEGRepresentation
這兩個方法得到的圖片資料都不是原圖片的真正資料
有沒有什麼辦法能將UIImagePickerController得到的圖片轉換成原始NSData的方法呢?
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 *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:[dic objectForKey:UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *asset)
{
ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef imgRef = [representation fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imgRef
scale:representation.scale
orientation:(UIImageOrientation)representation.orientation];
NSData
* <u><font color=
"\"red\""
>data</font></u> = UIImageJPEGRepresentation(image, 0.5);
}failureBlock:^(
NSError
*error){
NSLog
(@
"couldn't get asset: %@"
, error);
}
];
原文:http://blog.csdn.net/gdutxzy/article/details/44126437