iOS开发技巧之:获取ios相册gif图片 原数据

<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

猜你喜欢

转载自blog.csdn.net/vkooy/article/details/65442707