針對SDPhotoBrowser不支持保存gif圖片,提出的解決方法

最近在做圖片預覽和保存時候發現,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];

}





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