Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 调整图像大小以适应比例_Iphone_Ios_Uikit - Fatal编程技术网

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];