【问题标题】:UIImagePickercontroller not saving without use buttonUIImagePickercontroller 不使用按钮不保存
【发布时间】:2014-08-18 09:40:37
【问题描述】:

我正在使用 UIImagePickerController 拍照并保存到照片库。当我启动选择器时,它有拍照按钮和拍照后取消按钮,它显示 2 个按钮 Retake & use,如果我使用,使用按钮,然后只将图像保存到相册,但保存后我无法转到上一页或关闭选择器。

-(void)takepicture:(id)sender{


 // Create image picker controller
 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

 // Set source to the camera
 imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

 // Delegate is self
 imagePicker.delegate = self;



 // Show image picker
 [self presentModalViewController:imagePicker animated:YES];

 // [self performSelector:@selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];

 }


 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
 // Access the uncropped image from info dictionary
 UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

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

 // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

 [picker release];


}


 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
 [self dismissModalViewControllerAnimated:YES];
 }


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

 // Unable to save the image
 if (error)
 alert = [[UIAlertView alloc] initWithTitle:@"Error"
 message:@"Unable to save image to Photo Album."
 delegate:self cancelButtonTitle:@"Ok"
 otherButtonTitles:nil];
 else // All is well
 alert = [[UIAlertView alloc] initWithTitle:@"Success"
 message:@"Image saved to Photo Album."
 delegate:self cancelButtonTitle:@"Ok"
 otherButtonTitles:nil];
 [alert show];
 [alert release];

 [self performSelector:@selector(onTimer_Loadpicture:) withObject:nil afterDelay:0.5];

 }

【问题讨论】:

  • 你没有关闭 imagePickerController 中的视图控制器:didFinishPickingMediaWithInfo:
  • 你的意思是我需要添加 [self dismissModalViewControllerAnimated:YES];?
  • 是的,您需要在用户拍照后关闭视图。
  • 你可能还需要在 image:didFinishSavingWithError:contextInfo:

标签: ios objective-c uiimagepickercontroller


【解决方案1】:

只需关闭您的视图控制器。像这样,更新你的 didFinishPickingMediaWithInfo 方法

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
     {
     // Access the uncropped image from info dictionary
     UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

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

     // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    [picker dismissViewControllerAnimated:YES completion:NULL];


    }

【讨论】:

    【解决方案2】:

    您并没有关闭您的视图控制器。您应该这样做,如果不需要此代码的一部分,则由您决定是否要在 imageView 中检查您的图像。如果您不想检查那么只需删除 if 部分并使用 else 部分的代码。

        - (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo{
       [self dismissModalViewControllerAnimated:YES];
         _imgview.image = [editingInfo valueForKey:@"UIImagePickerControllerOriginalImage"];
        if(_imgview==nil){
           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Angry!!!"   message:@"Vennligst velg et bilde!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
    
            }
        else{
            _imgview.image = img;
             imageData=[self compressImage:img];
             [picker dismissModalViewControllerAnimated:YES];
            _lblname.hidden=true;
           }
      return;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-23
      • 2017-10-31
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多