iOS開発中のURL画像をローカルアルバムとアルバムに保存するアクセス権

3445 ワード

第1部


注意:このコードはbuttonクリックイベントに直接入れればいいです。

UIImageView *gtp = [[UIImageView alloc] init];
    ZGLPhotoModel *model = _modelArr[_currentPage];
    [gtp setImageWithURL:[NSURL URLWithString:model.raw_url]];
    UIImageWriteToSavedPhotosAlbum(gtp.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

第2部


注意:このコードは、デバイスがアルバムにアクセスする権限を取得し、画像が保存されたかどうかを示すことを実現します。

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {

    NSString *msg = nil ;
    if(error != NULL){
        msg = @" " ;
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" " message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel  =[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {


        }];
        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:^{

        }];

    }else{
        msg = @" ! " ;
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        /*   */
        picker.allowsEditing = YES;
        picker.delegate = self;
        /*  AlertAction */
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" " message:msg preferredStyle:UIAlertControllerStyleActionSheet];
        /*   */
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:picker animated:NO completion:^{}];
            }];

            [alert addAction:action1];
        }



        UIAlertAction *cancel  =[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {


        }];
        [alert addAction:cancel];

        [self presentViewController:alert animated:YES completion:^{

        }];


    }
}