系统相册中获取gif图片 保证取到的图片不会改变

时间:2024-01-02 18:51:14

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