iOS GIF 图片的 PHAsset 转 NSData
19-10-08 14:01
字数 2470
阅读 2942
已编辑
// 获取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 条评论
排序方式
时间
投票
快来抢占一楼吧
请登录后发表评论
相关推荐
文章归档
2024-11
1 篇
2024-06
1 篇
2024-05
2 篇
2024-04
2 篇
2024-03
2 篇
展开剩余 68 条
2024-01
1 篇
2023-10
1 篇
2023-09
1 篇
2023-08
1 篇
2023-06
1 篇
2023-04
1 篇
2022-12
2 篇
2022-06
1 篇
2022-04
4 篇
2022-03
3 篇
2022-01
6 篇
2021-12
2 篇
2021-11
2 篇
2021-10
2 篇
2021-09
1 篇
2021-08
2 篇
2021-07
4 篇
2021-06
1 篇
2021-05
3 篇
2021-04
3 篇
2021-01
2 篇
2020-11
1 篇
2020-10
3 篇
2020-09
2 篇
2020-08
1 篇
2020-07
5 篇
2020-06
5 篇
2020-05
1 篇
2020-04
1 篇
2020-03
2 篇
2020-02
3 篇
2020-01
1 篇
2019-11
5 篇
2019-10
10 篇
2019-09
12 篇
2019-08
17 篇
2019-07
8 篇
2019-05
3 篇
2019-04
8 篇
2019-03
7 篇
2019-02
8 篇
2019-01
5 篇
2018-12
7 篇
2018-11
8 篇
2018-10
4 篇
2018-09
7 篇
2018-08
12 篇
2018-07
9 篇
2018-06
6 篇
2018-05
11 篇
2018-04
18 篇
2018-03
1 篇
2018-02
2 篇
2018-01
10 篇
2017-12
14 篇
2017-11
44 篇
2017-10
13 篇
2017-09
4 篇
2017-08
12 篇
2017-07
5 篇
2017-06
4 篇
2017-05
2 篇
2017-04
3 篇
2017-03
9 篇
2017-02
3 篇
2017-01
2 篇
2016-12
10 篇
2016-11
4 篇
最新文章
最受欢迎
11-07 19:00
06-26 11:51
05-17 17:08
05-17 10:59
04-11 17:05
13 评论
11 评论
10 评论