如何在我的应用程序中拍摄图像并将图像保存在IOS中?

时间:2013-12-26 14:34:02

标签: ios image file camera photo

- (IBAction)btn_actionTakePicture:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];

    //I want to know how to save the image into the UIImage *image = savingImage so that I can implement the next 2 lines

    UIImage *image = savingImage;
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

最后两行代码依赖于将我捕捉的图像保存在变量savingImage中,那么我该如何实现呢?

2 个答案:

答案 0 :(得分:0)

您可以使用didFinishPickingMediaWithInfo。 来自官方文件:

  

要将静止图像保存到用户的相机胶卷相册,请从imagePickerController:didFinishPickingMediaWithInfo:方法的主体内调用UIImageWriteToSavedPhotosAlbum函数。

阅读详情:https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html

也许是这样的:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

答案 1 :(得分:-1)

您需要从IBAction中删除这两行并添加此功能

//UIImage *image = savingImage;
//UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

PS。你的UIImagePickerController *选择器需要是全局的 你可能会错过很多其他的东西。看一下Apple的示例代码https://developer.apple.com/library/ios/samplecode/photopicker/Introduction/Intro.html

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{

    NSLog(@"didFinishPickingImage ...");

    [picpicker dismissViewControllerAnimated:YES completion:nil];

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

}