美文网首页iOS常用iOS 技巧
ios 一键批量保存图片到本地相册

ios 一键批量保存图片到本地相册

作者: 肉肉要次肉 | 来源:发表于2020-09-07 16:47 被阅读0次

    -(void)saveAction{

    //这里我先模拟了一个图片数组

    self.tempImgArr = @[@"http://39.98.122.82/upload/home/c11.jpg",@"http://pic.bizhi360.com/bbpic/70/370.jpg",@"http://pic.bizhi360.com/bbpic/86/6386.jpg",@"http://pic.bizhi360.com/bbpic/69/6569.jpg"].mutableCopy;}

        self.imgArr = [NSMutableArray array];

        for(inti =0; i <self.tempImgArr.count; i++) {

            [self.imgArr  addObject:self.tempImgArr[i]];

        }

    //循环将图片加入一个数组里

        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

            // 2.判断用户的授权状态

            if (status == PHAuthorizationStatusNotDetermined) {

                // 如果状态是不确定的话,block中的内容会等到授权完成再调用

                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

                    // 授权完成就会调用

                    if(status ==PHAuthorizationStatusAuthorized) {

                    //调用存储图片的方法

                        [selfCWWSavePhotos];

                    }

                }];

                //如果允许访问

            }else if (status == PHAuthorizationStatusAuthorized) {

                //调用存储图片的方法

                [selfCWWSavePhotos];

                //如果权限是拒绝

            }else{

                //弹出一个页面提示用户去打开授权

    //            [MBProgressHUD showInWindowMessage:@"进入设置打开允许访问相册开关"];

            }

    - (void)CWWSavePhotos {

        if(self.imgArr.count>0) {

            [self saveimage:[self.imgArrobjectAtIndex:0]];

        }

    }

    #pragma mark- 保存图片的方法

    - (void)saveimage:(NSString*)imageUrl {

        NSURL*imgUrl = [NSURLURLWithString:imageUrl];

        [[SDWebImageDownloadersharedDownloader]downloadImageWithURL:imgUrlcompleted:^(UIImage*_Nullableimage,NSData*_Nullabledata,NSError*_Nullableerror,BOOLfinished) {

            if(image) {

                UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);

                //清除sd缓存,防止图片被sdwebimage缓存

                [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];

                [[SDImageCache sharedImageCache] clearMemory];

            }

        }];

    }

    #pragma mark-- <保存到相册>

    -(void)image:(UIImage*)imagedidFinishSavingWithError:(NSError*)errorcontextInfo:(void*)contextInfo {

        if(error){

            [selfalertView:@"保存图片失败"];

        }else{

            [self.imgArr removeObjectAtIndex:0];

            if(self.imgArr.count==0) {

              [selfalertView:@"图片已保存至您的相册"];

            }

        }

        [self CWWSavePhotos];

    }

    相关文章

      网友评论

        本文标题:ios 一键批量保存图片到本地相册

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