【发布时间】:2010-09-16 04:27:13
【问题描述】:
我有一个 UIImageView,目标是通过给它一个高度或宽度来按比例缩小它。
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//Add image view
[self.view addSubview:imageView];
//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;
图像确实已调整大小,但位置不在左上角。缩放 image/imageView 的最佳方法是什么?如何更正位置?
【问题讨论】:
-
我有一些类似于你的代码对我不起作用“UIImage *image = [UIImage imageNamed:imageString]; UIImageView *imageView = [[UIImage alloc] initWithImage:image];”引发一个异常,该异常会杀死我的应用程序,因为“由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'-[UIImage initWithImage:]: unrecognized selector sent to instance 0xd815930'”
-
@Spire 这是 UIImageView 不是 UIImage ^^
-
这对我有用。请记住将 .contentMode 设置为您的 UIImageView - 而不是您的 UIImage。 -- 这是我的: UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,30,30)]