UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
UIImageJPEGRepresentation(model.image, 0.5)
并不适用于gif的图片,最后根据ALAsset(IOS8之前)和PHAsset(iOS8之后)俩种对象,去转化成数据流;针对这俩种情况,解决方案如下:
if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
for (int i=0; i
if ([[UIDevice currentDevice].systemVersio***
* 选择相册图片(包括gif图片)
*
* @param assets PHAsset对象数组
*/
- (NSMutableArray *)selectedAlbumPhotosIncludingGifWithPHAssets:(NSArray*)assets {
NSMutableArray *imageArray = [NSMutableArray array];
CGSize targetSize = CGSizeMake(MAXFLOAT,MAXFLOAT);
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.resizeMode = PHImageRequestOptionsResizeModeFast;
options.synchronous = YES;
PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];
for (PHAsset *asset in assets) {
[imageManager requestImageDataForAsset:asset
options:options
resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
// DDLogDebug(@"dataUTI:%@",dataUTI);
//gif 图片
if ([dataUTI isEqualToString:(__bridge NSString *)kUTTypeGIF]) {
BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
if (downloadFinined && imageData) {
ImageListModel *model = [[ImageListModel alloc] init];
model.imageData = imageData;
model.image = [UIImage imageWithData:imageData];
model.isGif = YES;
[imageArray addObject:model];
NSDictionary * dic=@{@"type":@"1",@"imageData":model.imageData,@"image":model.image};
[_imageDataArray addObject:dic];
}
}
else {
//其他格式的图片,直接请求压缩后的图片
[imageManager requestImageForAsset:asset
targetSize:targetSize
contentMode:PHImageContentModeAspectFill
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
// 得到一张 UIImage,展示到界面上
NSNumber *isDegraded = info[PHImageResultIsDegradedKey];
if (!isDegraded.boolValue) {
// result = [self fixImageOrientation:result];
ImageListModel *model = [ImageListModel HModelInitWithUIImage:result];
[imageArray addObject:model];
NSDictionary * dic=@{@"type":@"2",@"imageData":@"" ,@"image":result};
[_imageDataArray addObject:dic];
}
}];
}
}];
}
Save(_imageDataArray, @"images");
return imageArray;
}
以上为对gif图片NSData化,下面为上传部分:
[[AFHTTPRequestOperationManager manager] POST:phoneStr parameters:dic constructingBodyWithBlock:^(id formData) {
NSString *timestamp = [NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]];
NSInteger index=0;
for (ImageListModel *model in _uploadImagesArray) {
if (model.isGif) { //gif图片
NSData *hdata=[NSData dataWithData:model.imageData];
GFLog(@"%@",hdata);
[formData appendPartWithFileData:model.imageData name:@"files" fileName:[NSString stringWithFormat:@"%@%@.gif",timestamp,@(index++)] mimeType:@"image/gif"];
GFLog(@"%@",(NSData *)model.imageData);
}
else
{
model.image=[self compressImageWith:model.image width:640.0 height:853];
[formData appendPartWithFileData:UIImageJPEGRepresentation(model.image, 0.5) name:@"files" fileName:[NSString stringWithFormat:@"%@_%@.png",timestamp,@(index++)] mimeType:@"image/png"];
}
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
此处为上传部分