目标 C:如何将图像放入 imageview
Posted
技术标签:
【中文标题】目标 C:如何将图像放入 imageview【英文标题】:Objective C: How to fit the image inside the imageview 【发布时间】:2017-06-07 11:00:28 【问题描述】:我正在使用 UIImagePickerController 为我的应用选择图像。但是我遇到了所选图像的纵横比问题。图像显示大于其原始尺寸并且未显示完整图像。我尝试将UIViewContentMode
属性更改为所有可能的值仍然没有用。任何人都可以指导我如何使它工作?
这里是 UIImagePickerController 委托的实现
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
self.imageProfile.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
【问题讨论】:
尝试使用 contentmodescaleaspectfill 向我们展示你对UIImagePickerControllerDelegate
的实现
根据您的图像宽度和宽高比调整您的图像高度,检查这个答案也许可以帮助您找出***.com/questions/44338392/…
@ReinierMelian 是的,我尝试将图像高度和宽度赋予图像视图..即使它没有显示完整图像。
@ZeenathYousuff 好的,我会尽力帮助你的,你正在使用自动布局吗?你也可以发布当前图像方面和想要的?
【参考方案1】:
当您使用自动布局时,首先使用您的图像高度约束定义一个出口,称为imageHeightConstraint
,然后使用这个修改您的代码
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
float aspectRatio = image.size.height/image.size.width
self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
self.imageProfile.image = image;
[self.imageHeightConstraint setConstant: self.imageProfile.frame.size.width * aspectRatio];
[picker dismissViewControllerAnimated:YES completion:nil];
已编辑
这是我的代码
#import "ViewController.h"
@interface ViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageProfile;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageConstraintHeigth;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
- (IBAction)selectImage:(id)sender
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
float aspectRatio = image.size.height/image.size.width;
self.imageProfile.contentMode = UIViewContentModeScaleAspectFit;
self.imageProfile.image = image;
//this if you need adjust heigth according to width
[self.imageConstraintHeigth setConstant: self.imageProfile.frame.size.width * aspectRatio];
[picker dismissViewControllerAnimated:YES completion:nil];
@end
这是我的限制
希望对我有帮助,对我来说效果很好
【讨论】:
@ZeenathYousuff 你有固定的宽度和高度约束吗?如果没有,你必须添加这些,因为 UIImageView 类具有固有的内容大小并且根据图像内容增长 我对宽度和高度都有固定的约束 @ZeenathYousuff 我会做我自己的例子,并发布我的结果 @ZeenathYousuff 我会做我自己的例子,然后发布我的结果,你需要你的图片以固定的宽度和高度显示,不是吗? @ZeenathYousuff 我的答案已被编辑,我需要进一步声明你必须等待你现在必须离开,但稍后我会再次检查这个答案以上是关于目标 C:如何将图像放入 imageview的主要内容,如果未能解决你的问题,请参考以下文章