iOS 从相册获取Gif 保存到沙盒 从沙盒读取Gif

2020-07-07  本文已影响0人  达若漠沙
#pragma mark - UIImagePickerController代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //获取选择的照片url
   NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    
    NSLog(@"%@",imageURL);
    __weak typeof(self) weakSelf = self;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:imageURL resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        Byte *buffer = (Byte*)malloc((unsigned long)rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:((unsigned long)rep.size) error:nil];
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
        //********  获取到照片的data,在这 里进行上传等操作
        YYImage *image = [YYImage imageWithData:data];
        weakSelf.imageView.image = image;
        
        //写入到文件
//      BOOL status = [data writeToFile:@"/Users/jiang/Desktop/xxx.gif" atomically:YES];
        
//        NSLog(@"%d",status);

    } failureBlock:nil];

    [picker dismissViewControllerAnimated:YES completion:nil];

}

将gif data数据保存到沙盒

 BOOL success =  [gifData writeToFile:[NSString stringWithFormat:@"%@/%ld",newPicPath,self.currentTag] atomically:NO];

从沙盒将gif data数据读出来

NSData *data = [NSData dataWithContentsOfFile:[[WMLayerManager sharedSingleton] getImgSandPath:_layer.imgName ]];

FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
上一篇下一篇

猜你喜欢

热点阅读