代码之家  ›  专栏  ›  技术社区  ›  Rui Peres

UIImagePickerController的视图大小错误

  •  4
  • Rui Peres  · 技术社区  · 12 年前

    我正在使用 UIImagePickerController UIView 作为我自己的子视图 UIViewController's UI视图 子视图。没什么特别的,当我拍照的时候问题就来了。正在发生的事情是这样的:

    1我从 UIImagePicker控制器 的视图如下:

    enter image description here

    2这就是我拍照时看到的( [self.imagePicker takePicture]; )然后把它放在 UIImageView :

    enter image description here

    从我看到的图像来看,在Y轴上有点高。有什么办法解决这个问题吗?我不在乎是否必须对以下图片进行某种裁剪:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
    

    当我使用时,我可能会理解这种行为 UIViewContentModeScaleAspectFill UI图像视图 contentMode ,但我希望能够指定要从哪里开始“查看”的Y。

    1 回复  |  直到 12 年前
        1
  •  4
  •   Rui Peres    12 年前

    28分钟后,我明白了这一点,哦,好吧,就这样:

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageTaken CGImage], CGRectMake(0.0f, 0.0f, 960.0f, 960.0f));
    
    [selectedImageView setImage:[UIImage imageWithCGImage:imageRef]];
    CGImageRelease(imageRef);
    

    通过在 960.0f 我能够保持图片的原始质量,也能够分辨出 Y 我可以把它从中“剪下来”。我还添加了以下内容:

    + (UIImage*)rotate:(UIImage*)image andOrientation:(UIImageOrientation)orientation;
    {
        UIGraphicsBeginImageContext(image.size);
    
        CGContextRef context=(UIGraphicsGetCurrentContext());
    
        if (orientation == UIImageOrientationRight) {
            CGContextRotateCTM (context, 90/180*M_PI) ;
        } else if (orientation == UIImageOrientationLeft) {
            CGContextRotateCTM (context, -90/180*M_PI);
        } else if (orientation == UIImageOrientationDown) {
            // NOTHING
        } else if (orientation == UIImageOrientationUp) {
            CGContextRotateCTM (context, 90/180*M_PI);
        }
    
        [image drawAtPoint:CGPointMake(0, 0)];
        UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    

    因为图片向左旋转。