iOS中gif圖片上傳的問題

首先說明下上傳gif圖片,必須得到圖片的NSData數據流,但是現有的直接轉圖片爲數據流的方法

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<assets.count; i++) {
                ALAsset * aset=assets[i];
                ALAssetRepresentation *rep = [aset defaultRepresentation];
                NSString * imageName=[rep filename];
                if ([[imageName componentsSeparatedByString:@"."][1]isEqualToString:@"GIF"]) {
                    Byte *imageBuffer = (Byte*)malloc(rep.size);
                    NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];
                    NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
                    
                    ImageListModel *model = [[ImageListModel alloc] init];
                    model.imageData = imageData;
                    
                    
                    model.isGif=YES;
                    model.image = [UIImage imageWithData:imageData];
                    
                    
                    NSDictionary * dic=@{@"type":@"1",@"imageData":model.imageData,@"image":model.image};
                    
                    
                    [_imageDataArray addObject:dic];
                     [_uploadImagesArray addObject:model];
                }
                else
                {
                    ImageListModel *model = [ImageListModel HModelInitWithUIImage:photos[i]];
                   
                    
                    NSDictionary * dic=@{@"type":@"2",@"imageData":@"" ,@"image":photos[i]};
                    [_imageDataArray addObject:dic];
                    [_uploadImagesArray addObject:model];
                    
                    
                    
                    
                }
                
  
            }
            
            Save(_imageDataArray, @"images");
  
            
            
            
        }
        else
        {
        
            NSMutableArray * array=[self selectedAlbumPhotosIncludingGifWithPHAssets:assets];
            for (int i=0; i<array.count; i++) {
                [_uploadImagesArray addObject:array[i]];
            }
        }
        
        
        
        
        






 if ([[UIDevice currentDevice].systemVersio<pre name="code" class="objc">***
*  選擇相冊圖片(包括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;
}

n floatValue] < 8.0) { for (int i=0; i<assets.count; i++) { ALAsset * aset=assets[i]; ALAssetRepresentation *rep = [aset defaultRepresentation]; NSString * imageName=[rep filename]; if ([[imageName componentsSeparatedByString:@"."][1]isEqualToString:@"GIF"]) { Byte *imageBuffer = (Byte*)malloc(rep.size); NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil]; NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES]; ImageListModel *model = [[ImageListModel alloc] init]; model.imageData = imageData; model.isGif=YES; model.image = [UIImage imageWithData:imageData]; NSDictionary * dic=@{@"type":@"1",@"imageData":model.imageData,@"image":model.image}; [_imageDataArray addObject:dic]; [_uploadImagesArray addObject:model]; } else { ImageListModel *model = [ImageListModel HModelInitWithUIImage:photos[i]]; NSDictionary * dic=@{@"type":@"2",@"imageData":@"" ,@"image":photos[i]}; [_imageDataArray addObject:dic]; [_uploadImagesArray addObject:model]; } } Save(_imageDataArray, @"images"); } else { NSMutableArray * array=[self selectedAlbumPhotosIncludingGifWithPHAssets:assets]; for (int i=0; i<array.count; i++) [_uploadImagesArray addObject:array[i]]; } }

以上爲對gif圖片NSData化,下面爲上傳部分:

 [[AFHTTPRequestOperationManager manager] POST:phoneStr  parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> 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) {
<span style="font-size:24px;">此處爲上傳部分</span>








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章