ios gif播放和存储

关于播放 需要pod 'FLAnimatedImage', '~> 1.0' 这个第三方库

NSURL*imgUrl = [NSURLURLWithString:@"图片的网址"];

    FLAnimatedImage *animatedImg = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:imgUrl]];

    FLAnimatedImageView *animatedImgView = [[FLAnimatedImageView alloc] init];

    animatedImgView.animatedImage= animatedImg;

    animatedImgView.frame=CGRectMake(0,0, photo.width, photo.height);

    [photo addSubview:animatedImgView];

photo 是要展示gif的背景view


关于保存gif到相册

#pragma mark - 异步处理

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        ////异步加载耗时操作

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"图片的网址"]];

        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

            [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:nil];

        }completionHandler:^(BOOLsuccess,NSError*_Nullableerror) {

            dispatch_async(dispatch_get_main_queue(), ^{

                NSString*msg =nil;

                if(success && !error){

                    msg =@"保存图片成功";

                }else

                    msg =@"保存图片失败";

                ZGPAlertShow(msg,@"确定");

            });

        }];


    });

你可能感兴趣的:(ios gif播放和存储)