Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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 显示UIImageview的顶部_Iphone_Ios_Objective C_Xcode_Uiimage - Fatal编程技术网

Iphone 显示UIImageview的顶部

Iphone 显示UIImageview的顶部,iphone,ios,objective-c,xcode,uiimage,Iphone,Ios,Objective C,Xcode,Uiimage,我正在尝试显示UIImage的顶部 我知道我可以使用以下代码并显示中间部分。我只是想弄清楚如何对顶部进行同样的操作: imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; 我从服务器获取的UIImage不一定大小相同,因此我无法使用UIViewContentModeTopLeft。我想要一种缩放图像并显示顶部的方法 谢谢。一种方法是在原始图像上调用CGImageCreat

我正在尝试显示UIImage的顶部

我知道我可以使用以下代码并显示中间部分。我只是想弄清楚如何对顶部进行同样的操作:

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
我从服务器获取的UIImage不一定大小相同,因此我无法使用UIViewContentModeTopLeft。我想要一种缩放图像并显示顶部的方法


谢谢。

一种方法是在原始图像上调用
CGImageCreateWithImageInRect
,创建一个只包含顶部的新图像

比如:

CGImageRef newImageRef = CGImageCreateWithImageInRect( oldImage.CGImage, CGRectMake(0,0,50,50) );
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease( newImageRef );

然后显示新图像而不是旧图像。

您希望显示图像的任意矩形,因此最简单的方法是将图像视图放在常规视图中。您可以设置图像视图的框架以显示所需的位,并设置封闭视图以进行剪裁

e、 g.您有一个1000 x 1000像素的图像,并且希望显示200到400400的矩形

UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SomeImage"]];
imageView.frame = CGRectMake( -200, -200, 1000, 1000);
UIView* enclosingView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 200, 200);
enclosingView.clipsToBounds = YES;
[enclosingView addSubview: imageView];
在这种情况下,内容模式无关紧要,因为您要将图像视图的维度设置为与图像匹配