oOOMIAO
oOOMIAO
2013-05-13 07:16
采纳率: 66%
浏览 8.9k
已采纳

在手机相册中保存图片失败

UIImagePicker 控制器,在点击相机按钮的时候,预览和使用按钮显示,但是图片没有保存在图片相册中。

代码:

-(void)viewDidAppear:(BOOL)animated{
    picker = [[UIImagePickerController alloc] init];
    // Set the image picker source:
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    // Hide the controls:
    picker.showsCameraControls = YES;
    picker.navigationBarHidden = YES;
    // Make camera view full screen:
    picker.wantsFullScreenLayout = YES;
    // Insert the overlay:
    picker.cameraOverlayView = self.view;
      self.view.backgroundColor=[UIColor clearColor];
    // Show the picker:
    [self presentModalViewController:picker animated:YES];
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • xinyoulingxi1_tong
    xinyoulingxi1_tong 2013-05-13 07:45
    已采纳

    这样设置:

    首先设置UIImagePickerController代理本身。

    -(void)viewDidAppear:(BOOL)animated{
        picker = [[UIImagePickerController alloc] init];
    picker.delegte = self ;
        // Set the image picker source:
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        // Hide the controls:
        picker.showsCameraControls = YES;
        picker.navigationBarHidden = YES;
        // Make camera view full screen:
        picker.wantsFullScreenLayout = YES;
        // Insert the overlay:
        picker.cameraOverlayView = self.view;
          self.view.backgroundColor=[UIColor clearColor];
    
        // Show the picker:
        [self presentModalViewController:picker animated:YES];
    }
        -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
            {
             UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
             UIImageWriteToSavedPhotosAlbum(image,self,  
                                               @selector(image:finishedSavingWithError:contextInfo:),
                                               nil);
            }
        - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
        {
            UIAlertView *alert;
            if (error)
                alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                   message:@"Unable to save image to Photo Album." 
                                                  delegate:self cancelButtonTitle:@"Ok" 
                                         otherButtonTitles:nil];
            else 
                alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                                   message:@"Image saved to Photo Album." 
                                                  delegate:self cancelButtonTitle:@"Ok" 
                                         otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    
    点赞 评论
  • zhanglei5415
    ReyZhang 2013-05-13 08:39

    你贴出来的代码仅是调用Camera.并没有编写在“Use”这张图片后所执行的回调。
    你还需要:
    1.在弹出Camera的控制器的头文件.h中添加两个代理协议

    @interface youViewController:UIViewController 
              <UINavigationControllerDelegate,UIImagePickerControllerDelegate>
    

    2.设置UIImagePickerController的代理为当前控制器

    picker.delegte = self ;
    

    只有加上了这一句,回调函数才会被执行

    3.在当前控制器中实现 UIImagePickerController的协议方法

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
            {
             UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
             UIImageWriteToSavedPhotosAlbum(image,self,  
                                               @selector(image:finishedSavingWithError:contextInfo:),
                                               nil); //保存图片到Album
            }
    
    点赞 评论

相关推荐