从相机胶卷加载的图像在纵向模式下拉伸过度

时间:2011-06-02 11:24:17

标签: iphone objective-c uiviewcontroller

我遇到了UIView的问题,它正在通过Camera Roll的图像进行摄取。

当我选择以纵向模式拍摄的照片时,视图总是将照片拉得太高,但是当我旋转设备时,照片拉伸得很好。


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if(info){
        {
            [artView removeFromSuperview];
            artView = nil;
            artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

            artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
            artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            artView.contentMode = UIViewContentModeScaleAspectFill;

[self.contentView addSubview:artView];

感谢任何帮助。

2 个答案:

答案 0 :(得分:5)

如果其他人仍然遇到此问题,请尝试在contentMode之后设置图片。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if (info) {
        [artView removeFromSuperview];
        artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

        artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        artView.contentMode = UIViewContentModeScaleAspectFill;
        artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

        [self.contentView addSubview:artView];
        [artView release];
    }
}

答案 1 :(得分:2)

你的代码看起来很好,除了它泄漏并且你有太多的开口括号和没有关闭括号。

您可能希望它看起来像这样:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if (info) {
        [artView removeFromSuperview];
        artView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

        artView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        artView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        artView.contentMode = UIViewContentModeScaleAspectFill;

        [self.contentView addSubview:artView];
        [artView release];
    }
}

UIViewContentModeScaleAspectFill不应该拉伸图像。它应该剪辑它。也许UIViewContentModeScaleAspectFit正是你要找的?