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