UIImagePickerController imagePicker不适合iPhone 4屏幕

时间:2011-06-27 09:23:40

标签: iphone ios uiimage uiimagepickercontroller retina-display

我让用户使用UIImagePickerControllerSourceTypePhotoLibrary选择图像。

然后我有以下方法获取编辑后的图像,应用叠加,然后将其传递给我的PostPictureViewController进行显示。在显示器上,我希望图像保持其原始宽高比并使其适合屏幕。

在使用4.3 sdk的模拟器上工作正常,但是当我把它放在iPhone 4视网膜显示屏上时,图像会缩放到左上角,只有1/4的图像全屏显示,摆脱了原始宽高比!

如何将其缩小以适应屏幕,就像在模拟器上工作一样?

我还试图完全摆脱叠加层,只是将原始图像和已编辑的图像返回显示,但即​​使这样也不能保持其原始宽高比!

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{        
    // Access the cropped image from info dictionary
    image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    // Combine image with overlay before saving!!
    image = [self addOverlayToImage:image];

    overlayGraphicView.image = nil;

    // Take the picture image to the post picture view controller
    postPictureView = [[PostPictureViewController alloc] init:image];    
    [picker pushViewController:postPictureView animated:YES];

    [picker release],picker = nil;
}

这是我的添加叠加图像功能:

- (UIImage*) addOverlayToImage:(UIImage*)originalImage
{
    //CGRect cgRect =[[UIScreen mainScreen] bounds];
    CGSize size = originalImage.size;

    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        // Use for iPhone 4 Retina
        UIGraphicsBeginImageContextWithOptions(size, YES, 0.0);
    } else {
        UIGraphicsBeginImageContext(size);
    }

    UIImage* overlayImage = [UIImage imageNamed:overlayGraphicName];

    [originalImage drawAtPoint:CGPointMake(0,0)];

    [(UIImage *)overlayImage drawAtPoint:CGPointMake(0,(originalImage.size.height/2 - overlayImage.size.height/2) + 20)];

    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    [finalImage retain];

    UIGraphicsEndImageContext();

    return finalImage;
}

最后在我的PostPictureViewController中,我在viewdidload中显示图像:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor blackColor]];

    // Load the picker image for viewing
    UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320, 480)];
    [pickerView setImage:pickerImage];
    pickerView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:pickerView];
    [pickerView release],pickerView=nil;
}

在这个用于显示图像的viewdidload方法中,如果我这样做:

UIImageView *pickerView = [[UIImageView alloc] initWithImage:pickerImage];

然后图像不会在模拟器上垂直居中到屏幕,并从0,0开始,其中实际定义框架宽度和高度,CGRectMake将其置于屏幕上。

显然,对于iPhone 4,我也试过改变它:

UIImageView *pickerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 640, 960)];

但是图像仍然比屏幕更大,更大!

1 个答案:

答案 0 :(得分:1)

您可以在界面构建器中将imageview的属性设置为aspectfit。这应该有帮助。或者通过编程可以使用 imageView.contentMode = UIViewContentModeScaleAspectFit;