ios - CGRect 图像调整大小

标签 ios objective-c uiimage

我正在使用 CGRect 来显示图像。我希望 CGRect 使用图像的宽度和高度,而无需我指定它。

可以这样吗:

CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);

变成这样:

CGRectMake(0.0f, 40.0f, myImage.width, myImage.height);

当我指定参数时,有些图像会变形。

代码如下:

CGRect myImageRect = CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:recipe.img]];

感谢您的帮助。

最佳答案

一旦你有了一个 UIImage,你可以通过查看 size 来找到它的大小。属性:

UIImage * image = [UIImage imageNamed:recipe.img];
CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];

关于ios - CGRect 图像调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632042/

相关文章:

ios - 旋转UIImage而不会降低质量

ios - 如何以编程方式+ iPhone获取.png文件的名称

ios - 将 UIImage 以二进制形式保存到 SQLite 并使用 imageWithData 检索它

iphone - 对象旋转不正确 - iPhone 应用程序

ios - 在 Xcode 4.3 上为模拟器构建 OpenSSL 失败

iphone - 如何开发认证以符合苹果的条款和条件?

ios - 如果类中的实例变量的值已更改,如何触发断点?

iphone - 如何判断自 NSDate 以来的小时数

ios - SDWebImage 的缓存在哪里?

objective-c - 更改 navigationController.delegate 会导致访问错误