【问题标题】:picture taken with camera doesn't resize in uiimageview用相机拍摄的照片不会在 uiimageview 中调整大小
【发布时间】:2014-12-08 21:52:35
【问题描述】:

我的代码

- (IBAction)takePhoto:(UIButton *)sender {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
    UIImagePickerController *picker = [UIImagePickerController new];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:nil];
}else{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR!" message:@"Device has no camera!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
}

和代表

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.userImage.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

嗯,这非常简单和基本。我的问题是,在将我的图像分配给我拥有的 UIImageView 之后(它具有固定/固定的宽度和高度),而不是将自身调整为 uiimageview 的大小,它在我的整个视图中延伸,更多地作为图像放置的背景它应该在哪里。同样,我的 uiimageview 的宽度和高度已被固定。有人可以帮忙吗?谢谢

【问题讨论】:

    标签: ios objective-c uiimageview


    【解决方案1】:

    UIImageView 的 contentMode 应该是 UIViewContentModeScaleAspectFit,而不是看起来的 UIViewContentModeCenter。

    如果您需要即时更改内容模式,只需添加以下行:

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        self.userImage.image = chosenImage;
    
        // Add this line
        self.userImage.contentMode = UIViewContentModeScaleAspectFit;
    
        [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    

    【讨论】:

    • 不确定。这很可能是原始问题的答案。
    • 为什么我的问题会被否决?有什么问题吗。显然我会删除这个问题,但这不是很“社区”-like
    • 不,您的问题似乎很好,并向我解释了。你试过我告诉你的吗?
    • 是的,我应该将我的评论再保存一分钟:) 它可以工作......但我将模式设置为“中心”的原因是因为我有一张标准的“相机”图片,在如果用户没有选择拍摄或选择照片。使用方面拟合有点搞砸了它的美学。我将尝试通过检查图像是否设置为标准来解决这个问题。再说一次,我很遗憾倾向于删除,因为我很投入......
    • 然后即时更改内容模式。请参阅我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2013-09-30
    • 2022-11-05
    相关资源
    最近更新 更多