针对SDPhotoBrowser不支持保存gif图片,提出的解决方法

夏俊人
2023-12-01

最近在做图片预览和保存时候发现,SDPhotoBrowser保存的gif图片,竟变成了静态的gif图片,原以为iOS系统相册不支持GIF的播放,也就没注意,最后在微信发图的时候发现的确变成了静态gif图片,才知道这个出现了问题,在这个库里保存的是图片,而保存gif时候要保存数据流才不会变成静态图,最后解决方法如下:

- (void)saveImage

{

    int index = _scrollView.contentOffset.x / _scrollView.bounds.size.width;

    

    

    /*

     为了支持gif图片的存储此处改动原第三方库的内容如下:

     与原库比对可发现

        原来的

     

     

     

     */

    

//    UIImageView *currentImageView = _scrollView.subviews[index];

    

   // UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

    


    NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[self highQualityImageURLForIndex:index]]];

    

    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f) {

            

            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

                PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];

                options.shouldMoveFile = YES;

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

            } completionHandler:^(BOOL success, NSError * _Nullable error) {

                dispatch_sync(dispatch_get_main_queue(), ^{

                    //保存成功

                    

           // [self image:<#(UIImage *)#> didFinishSavingWithError:<#(NSError *)#> contextInfo:<#(void *)#>]

                    [self didFinishSavingWithError:error];

                    

                });

            }];

        }

        else {

            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

            [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

                dispatch_sync(dispatch_get_main_queue(), ^{

                    //保存成功

                     [self didFinishSavingWithError:error];

                });

            }];

        }

    }];

    

    

    

    

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];

    indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

    indicator.center = self.center;

    _indicatorView = indicator;

    [[UIApplication sharedApplication].keyWindow addSubview:indicator];

    [indicator startAnimating];

}

//保存结果提示语的出现以及indicator的消失

- (void)didFinishSavingWithError:(NSError *)error;

{

    [_indicatorView removeFromSuperview];

    

    UILabel *label = [[UILabel alloc] init];

    label.textColor = [UIColor whiteColor];

    label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];

    label.layer.cornerRadius = 5;

    label.clipsToBounds = YES;

    label.bounds = CGRectMake(0, 0, 150, 30);

    label.center = self.center;

    label.textAlignment = NSTextAlignmentCenter;

    label.font = [UIFont boldSystemFontOfSize:17];

    [[UIApplication sharedApplication].keyWindow addSubview:label];

    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];

    if (error) {

        label.text = SDPhotoBrowserSaveImageFailText;

    }   else {

        label.text = SDPhotoBrowserSaveImageSuccessText;

    }

    [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];

}





 类似资料: