美文网首页
iOS GIF图片处理汇总

iOS GIF图片处理汇总

作者: 幻想无极 | 来源:发表于2017-11-24 17:51 被阅读315次

    前言

    现在做项目要新加gif的上传,显示和保存到相册,之前以为挺简单的就改个后缀,💔💔💔

    gif的上传

    1.从相册获取gif图片

    先判断获取的图片中有没有gif进行不同的业务逻辑处理,比如图片会加滤镜之类的

     [WJTZImageManager showGifImagePhotoWithController:self count:9 finishBlock:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                   
                    if ([WJTZImageManager haveGifWithAssetArray:assets]) {
                        [myself gotoSayPublishControllerWithImageArray:photos assetArray:assets type:MouoPublishSayType];
                    }else {
                        [myself gotoFilterControllerWithImageArray:photos assetArray:assets type:MouoPublishSayType];
                    }
                   
                }];
    
    2.NSData数据上传

    1.系统自带的UIImage里面转成NSData类型就不是图了所以我们要用以下的转换方法

    - (void)getOriginalPhotoDataWithAsset:(id)asset completion:(void (^)(NSData *data,NSDictionary *info,BOOL isDegraded))completion {
        if ([asset isKindOfClass:[PHAsset class]]) {
            PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
            option.networkAccessAllowed = YES;
            option.resizeMode = PHImageRequestOptionsResizeModeFast;
            [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
                BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
                if (downloadFinined && imageData) {
                    if (completion) completion(imageData,info,NO);
                }
            }];
        } else if ([asset isKindOfClass:[ALAsset class]]) {
            ALAsset *alAsset = (ALAsset *)asset;
            ALAssetRepresentation *assetRep = [alAsset defaultRepresentation];
            Byte *imageBuffer = (Byte *)malloc(assetRep.size);
            NSUInteger bufferSize = [assetRep getBytes:imageBuffer fromOffset:0.0 length:assetRep.size error:nil];
            NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
            if (completion) completion(imageData,nil,NO);
        }
    }
    
    

    2.由于是异步的,需要线程同步,我这些了一个方法

    + (void)getDataWithArray:(NSArray *)array finish:(void(^)(NSArray *array))finish {
        
        NSMutableArray *dataArray = [NSMutableArray array];
        NSMutableArray *assetArray = [NSMutableArray array];
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIImage class]]) {
                [dataArray addObject:UIImageJPEGRepresentation(obj, 0.8)];
            }
            if ([obj isKindOfClass:[NSData class]]) {
                [dataArray addObject:obj];
            }
            if ([obj isKindOfClass:[PHAsset class]]) {
                [assetArray addObject:obj];
            }
            
        }];
        dispatch_group_t group = dispatch_group_create();
        [assetArray enumerateObjectsUsingBlock:^(PHAsset *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
                dispatch_group_enter(group);
            [[TZImageManager manager] getOriginalPhotoDataWithAsset:obj completion:^(NSData *data, NSDictionary *info, BOOL isDegraded) {
    
                [dataArray addObject:data];
                dispatch_group_leave(group);
            }];
        }];
        dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
            
            finish(dataArray);
        });
        
    }
    
    

    3.拿着数据进行上传就行了,最好名称后缀再做个判断是GIF的上传的时候后缀改为.gif,这样的话,如果项目有需要判断GIF的情况可用地址后缀判断

    gif保存到本地相册

    1.获取网络请求后的NSData

    现在网络请请求加载图片一般用SDWebImage和YYWebImage
    这边举例YYWebImage
    1.使用YYImageCache,传入网络图片地址获取图片缓存

    - (nullable NSData *)getImageDataForKey:(NSString *)key;
    - (void)getImageDataForKey:(NSString *)key
                     withBlock:(void(^)(NSData * _Nullable imageData))block;
    

    2.使用YYAnimatedImageView播放GIF的话可以直接抓出NSData数据

    YYImage *image = (YYImage *)imageView.image;
    //image.animatedImageData
    //@property (nullable, nonatomic, readonly) NSData *animatedImageData;
    
    2.将NSData保存到本地相册
    - (void)savePhotoWithData:(NSData *)data completion:(void (^)(NSError *error))completion {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    
            PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
            options.shouldMoveFile = YES;
            PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
            [request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
            request.creationDate = [NSDate date];
            
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            dispatch_sync(dispatch_get_main_queue(), ^{
                if (success && completion) {
                    completion(nil);
                } else if (error) {
                    NSLog(@"保存照片出错:%@",error.localizedDescription);
                    if (completion) {
                        completion(error);
                    }
                }
            });
        }];
    }
    

    gif的显示

    网上很多第三方框架,这边推荐一个YYAnimatedImageView

    gif的压缩

    http://www.jianshu.com/p/867f96ffa136

    相关文章

      网友评论

          本文标题:iOS GIF图片处理汇总

          本文链接:https://www.haomeiwen.com/subject/wtyfbxtx.html