objective-c - 如何按比例缩放 UIImageView?

我有一个 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;

图像确实已调整大小,但位置不在左上角。缩放图像/ ImageView 的最佳方法是什么?如何更正位置?

最佳答案

一旦找到文档,就可以轻松修复!

 imageView.contentMode = .scaleAspectFit

https://stackoverflow.com/questions/185652/

相关文章:

ios - 将 NSURL 转换为 NSString

objective-c - 对 Objective-C 代码进行单元测试的最佳方法是什么?

ios - 在 Xcode 6 中使用 AutoLayout 约束模拟方面适合行为

ios - 如何在 Swift 中发出 HTTP 请求?

objective-c - NSString 属性 : copy or retain?

objective-c - 使用 alloc init 而不是 new

objective-c - 我的 Objective-C 单例应该是什么样的?

objective-c - 在不使用私有(private) API 的情况下获取当前的第一响应者

ios - 何时使用 NSInteger 与 int

ios - 在 Objective-C 中使用 GCD 的 dispatch_once 创建单例