# iOS GIF 图片的 PHAsset 转 NSData

**URL:** <https://www.shiqidu.com/t/topic/651>\
**Category:** 开发调优\
**Tags:** C-C-Obj-C\
**Created:** [2019 年10 月 8 日 06:01 UTC](https://www.shiqidu.com/t/topic/651 "2019-10-08T06:01:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2019 年10 月 8 日 06:01 UTC](https://www.shiqidu.com/t/topic/651/1 "2019-10-08T06:01:33Z")

</div>

```OC
// 获取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:^(){
        }];
    }
}

```
