iOS GIF 图片的 PHAsset 转 NSData

10-08 14:01 字数 2470 阅读 76 已编辑
// 获取gif图片对应的PHAsset之后
// eg:
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    // 如果系统版本大于iOS8,asset是PHAsset类的对象,否则是ALAsset类的对象
    __blockNSMutableArray *datas = [[NSMutableArrayalloc] init];
    [assets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger index, BOOL *innerStop) {
        if (asset) {
            UIImage *thumbnail = photos[index];
            // UIImage *thumbnail =  [UIImage imageWithCGImage:[alAsset thumbnail]];
            // PHAsset 没有thumbnail 这个方法 用的photos
            NSMutableDictionary *data = [NSMutableDictionary dictionaryWithDictionary:@{ @"type":@"album", @"thumb":thumbnail, @"isUploaded" : @"0"}];

            NSArray *resourceList = [PHAssetResource assetResourcesForAsset:asset];
            [resourceList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                PHAssetResource *resource = obj;
                PHAssetResourceRequestOptions *option = [[PHAssetResourceRequestOptionsalloc]init];
                option.networkAccessAllowed = YES;
                // 首先,需要获取沙盒路径
                NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
                // 拼接图片名为resource.originalFilename的路径
                NSString *imageFilePath = [path stringByAppendingPathComponent:resource.originalFilename];
                [data setValue:imageFilePath forKey:@"path"];

                if ([resource.uniformTypeIdentifier isEqualToString:@"com.compuserve.gif"]) {
                    NSLog(@"为gif");

                    __block NSData *datagif = [[NSData alloc]init];
                    [[PHAssetResourceManagerdefaultManager] writeDataForAssetResource:resource toFile:[NSURLfileURLWithPath:imageFilePath]  options:option completionHandler:^(NSError * _Nullable error) {
                        if (error) {
                            // NSLog(@"error:%@",error);
                            if(error.code == -1){//文件已存在
                                datagif = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imageFilePath]];
                            }
                            // NSLog(@"datagif%@",datagif);
                            [data setValue:datagif forKey:@"scale_image"];
                            [data setValue:@1 forKey:@"is_gif"];
                        } else {
                            datagif = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imageFilePath]];
                            // NSLog(@"datagif%@",datagif);
                            [data setValue:datagif forKey:@"scale_image"];
                            [data setValue:@1 forKey:@"is_gif"];
                        }
                    }];
                }else{
                    NSLog(@"jepg");
                    UIImage *originalImage = photos[index];
                    [data setValue:originalImage forKey:@"scale_image"];
                }
                [datas addObject:data];
            }];
        }
    }];

    if ([datas count] > 0) {
        DhFeedPublishController *vc = [[DhFeedPublishControlleralloc] init];
        vc.mDatas = [[NSMutableArray alloc] initWithArray:datas];
        DhNavigationController *nav = [[DhNavigationControlleralloc] initWithRootViewController:vc];
        // for ios 7
        [self presentViewController:nav animated:YEScompletion:nil];
        [picker dismissViewControllerAnimated:YEScompletion:^(){
        }];
    }
}
1人点赞>
关注 收藏 改进 举报
0 条评论
排序方式 时间 投票
快来抢占一楼吧
请登录后发表评论