9

当我使用 UIImagePickerController 拍照时,屏幕上显示的图片多于屏幕上可见的图片......我怎样才能拍照/裁剪它,以便它与我在屏幕上看到的完全一样?

编辑:我将添加代码,以指定我的问题:

- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.showsCameraControls = NO;
imagePickerController.allowsEditing = YES;

if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
    [[NSBundle mainBundle] loadNibNamed:@"OverlayView" owner:self options:nil];
    self.overlayView.frame = imagePickerController.cameraOverlayView.frame;
    imagePickerController.cameraOverlayView = self.overlayView;
    //self.overlayView = nil;
    self.overlayImage.image = self.imageView.image;
    self.overlayImage.alpha = 0.5;

    UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(60, 80, 200, 30)];
    [slider setMaximumValue:1.0];
    [slider setMinimumValue:0.0];
    [slider setValue:0.5];
    [slider addTarget:self action:@selector(sliderChanged:)  forControlEvents:UIControlEventValueChanged];

    if (self.working == TRUE)
    {
        [imagePickerController.cameraOverlayView addSubview:self.overlayImage];
        [imagePickerController.cameraOverlayView addSubview:slider];
    }
}

self.imagePickerController = imagePickerController;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}

didFinishPickingMediaWithInfo:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];

//this works, image is displayed
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//this doesn't work, image is nil
UIImage *image = [info objecyForKey:UIImagePickerControllerEditedImage];    

[self.imageView setImage:image];
self.working = TRUE;

self.imagePickerController = nil;
//[self dismissViewControllerAnimated:YES completion:NULL];
}

我可以在什么时候使用 UIImagePickerControllerEditedImageimagePickerController.showsCameraControls = NO;吗?

4

3 回答 3

10

您可以根据信息字典的键获取原始图像或裁剪图像。

pickedImageEdited将是您正在寻找的裁剪图像,并且pickedImageOriginal将是完整的原始图像。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickedImageOriginal = [info objectForKey:UIImagePickerControllerOriginalImage];

    UIImage *pickedImageEdited = [info objectForKey:UIImagePickerControllerEditedImage];

    //do your stuff

    [self dismissViewControllerAnimated:YES completion:nil];
}
于 2013-09-21T11:46:34.037 回答
9

只是将其发布为答案,但我不确定海报是否要求编辑图像。

当您使用 UIImagePickerController 拍照时,选择器仅显示拍照时实际捕获的部分图像,如下图所示,其中黑线是 iPhone 的屏幕。

在此处输入图像描述

您从相机获得的图像是全尺寸图像,但屏幕上只是图像的中心,图像的宽度或高度都达到了屏幕尺寸。

您需要做的就是从上图所示的图像中获取中心,然后您就可以在屏幕上看到与您完全相同的图像。

于 2013-09-24T19:12:55.097 回答
5

在 Wim 的回答之后,我能够想出这样的解决方案:

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

UIGraphicsBeginImageContext(CGSizeMake(720, 960));
[image drawInRect: CGRectMake(0, 0, 720, 960)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect cropRect = CGRectMake(40, 0, 640, 960);

CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
[self.imageView setImage:[UIImage imageWithCGImage:imageRef]];

如您所见,这是为 3,5" iPhone 屏幕制作的,它似乎可以工作,但代码取决于设备,还是有更好的解决方案?

于 2013-09-25T20:01:16.510 回答