Iphone 调整图像大小以适应比例
在我的应用程序设计中,我有一个正方形的空间来放置来自远程服务器的图像。但是,有时图像是一个横向矩形而不是正方形Iphone 调整图像大小以适应比例,iphone,ios,uikit,Iphone,Ios,Uikit,在我的应用程序设计中,我有一个正方形的空间来放置来自远程服务器的图像。但是,有时图像是一个横向矩形而不是正方形 我不想裁剪图像,而是将其缩小到足以容纳正方形约束的范围内,然后用一些背景色(可能是白色)填充剩余的空间。将UIImageView的contentMode设置为uiviewcontentmodescalespectfit我不知道这是否有帮助,但是我使用它来适应视图的大小以匹配图像。它获取提供的rect,并对其进行修剪以匹配图像。然后可以将返回的CGRect应用于视图。我使用它是为了给图像
我不想裁剪图像,而是将其缩小到足以容纳正方形约束的范围内,然后用一些背景色(可能是白色)填充剩余的空间。将
UIImageView的contentMode
设置为uiviewcontentmodescalespectfit
我不知道这是否有帮助,但是我使用它来适应视图的大小以匹配图像。它获取提供的rect,并对其进行修剪以匹配图像。然后可以将返回的CGRect应用于视图。我使用它是为了给图像添加一个阴影(如果视图与图像不完全匹配,这看起来是错误的)
}创建具有所需颜色背景色的UIImageView。从服务器创建图像,在图像视图中设置图像,然后设置contentMode=ui视图contentmodescaleaspectfit
UIImageView *backgroundColorWhite = [[UIImageView alloc] initWithFrame:someObject.frame];
backgroundColorWhite.backgroundColor = [UIColor whiteColor];
UIImage *serverImage = [UIImage imageWithData:serverData];
backgroundColorWhite.contentMode = UIViewContentModeScaleAspectFit;
[backgroundColorWhite setImage:serverImage];
UIImageView *backgroundColorWhite = [[UIImageView alloc] initWithFrame:someObject.frame];
backgroundColorWhite.backgroundColor = [UIColor whiteColor];
UIImage *serverImage = [UIImage imageWithData:serverData];
backgroundColorWhite.contentMode = UIViewContentModeScaleAspectFit;
[backgroundColorWhite setImage:serverImage];