UIImagePickercontroller没有使用按钮保存

时间:2014-08-18 09:40:37

标签: ios objective-c uiimagepickercontroller

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

-(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];

 }

2 个答案:

答案 0 :(得分:3)

只是关闭你的视图控制器。像这样,更新你的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];


    }

答案 1 :(得分:0)

你没有解雇你的视图控制器。你应该这样做,如果这部分代码不是必需的,那么你想要在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;
 }