如何裁剪UIImagePickerController拍摄的照片,因为它正在显示

时间:2013-09-21 11:36:36

标签: ios objective-c uiimagepickercontroller crop

当我使用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];
}

imagePickerController.showsCameraControls = NO;时可以使用UIImagePickerControllerEditedImage吗?

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:8)

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

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

enter image description here

从相机获取的图像是全尺寸图像,但屏幕上只是图像的中心,图像的宽度或高度最大化为屏幕尺寸。

您需要做的就是从图像中获取中心,如上图所示,您可以获得屏幕上的确切图像。

答案 2 :(得分:4)

在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屏幕制作的,它似乎有用,但代码依赖于设备,还是有更好的解决方案?