我有一个UIImageView
的视图。我正从服务器检索宽300像素,高100像素的图像。但是,当我将图像插入UIImageView
时,图像会被拉伸到图像视图的大小。
代码viewDidLoad:
id path = @"URL TO IMAGE";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
CGRect frame = [imageView frame];
frame.size.width = 300;
frame.size.height = 100;
[imageView setFrame:frame];
self.imageView.image = img;
但是,UIImageView
尺寸未更改为上述代码第6行和第7行中的值。
我感谢任何建议,我对Objective-C很新。
·H:
#import <UIKit/UIKit.h>
@interface detailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
答案 0 :(得分:3)
您需要设置UIImageView的contentMode
。查看contentMode
的UIView文档,查看可以使用的不同值。例如,UIViewContentModeScaleToFill
拉伸图像以完全匹配UIImageView的大小; UIViewContentModeCenter
根本不会改变图像的大小;等等。
setFrame:
将无效,因为该帧由约束决定。