iWi*_*ard 8 iphone objective-c uiimageview uiimage ios
我UIImageView
在其中显示50x100图像.
我想只显示图像50x50(顶部)的一部分?
我怎样才能做到这一点?
mal*_*lex 13
在UIImageView中移动大图像的非常简单的方法如下.
让我们得到大小(100,400)的图像,代表一个图像的4个状态,一个在另一个之下.我们想在尺寸为(100,100)的方形UIImageView中显示偏移Y = 100的第二张图片.解决方案是:
UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
CGRect contentFrame = CGRectMake(0, 0.25, 1, 0.25);
iView.layer.contentsRect = contentFrame;
iView.image = [UIImage imageNamed:@"NAME"];
Run Code Online (Sandbox Code Playgroud)
这里contentFrame是相对于真实UIImage大小的规范化帧.因此,"0"表示我们从左边界开始可见的图像部分,"0.25"表示我们有垂直偏移100,"1"表示我们想要显示图像的全宽,最后,"0.25"表示我们想要只显示高度的1/4部分图像.
因此,在局部图像坐标中,我们显示以下帧
CGRect visibleAbsoluteFrame = CGRectMake(0*100, 0.25*400, 1*100, 0.25*400)
or CGRectMake(0, 100, 100, 100);
Run Code Online (Sandbox Code Playgroud)
ser*_*gio 11
你可以使用CGImageCreateWithImageInRect
,这是在CGImageRef上工作的Quartz原语来裁剪图像,所以你会有类似的东西:
CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropRect);
UIImage* outImage = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation]];
CGImageRelease(imageRef);
Run Code Online (Sandbox Code Playgroud)
计算时cropRect
,请记住它应以像素为单位,而不是以点为单位,即:
float scale = originalImage.scale;
CGRect cropRect = CGRectMake(0, 0,
originalImage.size.width * scale, originalImage.size.height * 0.5 * scale);
Run Code Online (Sandbox Code Playgroud)
其中0.5
的因素占了,你只想要上半部分的事实.
如果您不想进入低级别,可以将图像添加UIView
为背景颜色并使用clipToBounds
CALayer属性进行剪切:
myView.backgroundColor = [UIColor colorWithBackgroundPattern:myImage];
myView.layer.clipToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
另外,相应地设置myView
边界.
我可能有一个解决方案给你.看看这是否有效.在Interface Builder中,有一个关于图像内容填充属性的选项.您可以将其设置为top-left
.按照界面构建器中的图像 -
在此之后设置大小UIImageView
为50x50并选中"clip-subviews"...
归档时间: |
|
查看次数: |
10587 次 |
最近记录: |