如何将摄像头拍摄的图像(UIImagePickerControllerSourceTypeCamera)转换为iOS中的NSData

时间:2015-03-09 08:16:37

标签: ios objective-c uiimageview uiimagepickercontroller nsdata

我使用的代码:

-(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self presentModalViewController:picker animated:YES];
}

以下是在图像视图中使用UIImagePickerControllerSourceTypeCamera拍摄图像的功能:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

3 个答案:

答案 0 :(得分:2)

试试这个

UIImage *img          = [info objectForKey:UIImagePickerControllerOriginalImage];
img                   = [info valueForKey:UIImagePickerControllerEditedImage];
self.imageView.image  = img;
NSData *imageData     = UIImagePNGRepresentation(img);

这样可行。

答案 1 :(得分:0)

获取图像的NSData

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *imageData = UIImagePNGRepresentation(image);

答案 2 :(得分:0)

像这样转换,

NSData *imageData = UIImagePNGRepresentation(image);

如果您想转换回来,

UIImage *image= [UIImage imageWithData:imagedata];