获取ios相册gif图片 原数据

原文:http://www.cocoachina.com/bbs/read.php?tid-151776-keyword-gif%7Cdata.html

<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);
              }
              ];



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值