我想改变我的UIImageView高度/宽度以匹配我从相册中选择的图像,我在这里找到了一个代码片段.
-(CGRect)frameForImage:(UIImage *)imgs inImageViewAspectFit:(UIImageView *)imagev
{
float imageRatio = imgs.size.width / imgs.size.height;
float viewRatio = imagev.frame.size.width / imagev.frame.size.height;
if(imageRatio < viewRatio)
{
float scale = imagev.frame.size.height / imgs.size.height;
float width = scale * imgs.size.width;
float topLeftX = (imagev.frame.size.width - width) * 0.5;
NSLog(@"image after aspect fit: width=%f",width);
imagev.frame = CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
return CGRectMake(topLeftX, 0, width, imagev.frame.size.height);
}
else
{
float scale = imagev.frame.size.width / imgs.size.width;
float height = scale * imgs.size.height;
float topLeftY = (imagev.frame.size.height - height) * …Run Code Online (Sandbox Code Playgroud) 我正在框架中制作图片,然后用户可以在运行时调整帧大小.为了这个目的,我已经UIView在我的主要UIViewController内部和内部做了一个习惯,我UIView放置了2 UIImageView.在xCode的Document Outline Pane中,它的层次结构是:
查看
| --ImageView
| --ImageView
我正在使用这行代码来改变UIView不同条件下的大小,它会相应调整大小:
self.imageObject.frame = CGRectMake(self.imageObject.frame.origin.x ,
self.imageObject.frame.origin.y,
200,
200);
Run Code Online (Sandbox Code Playgroud)
imageObject是我的UIView.当我运行它时,UIView更改的大小,但UIImageView大小保持不变.我也试过为我做同样的事情,UIImageView但它没有用.