简体   繁体   English

如何创建一个非矩形的UIImageView

[英]How to create a non-rectangular UIImageView

I'm trying to make a ViewController that presents info from a webpage like this: 我试图做一个ViewController来从这样的网页上显示信息:

http://i.stack.imgur.com/TH6iV.jpg

However, I'm confused on one thing. 但是,我对一件事感到困惑。 How did they get the imageView to display an image that's cut off at the corner, ie not rectangular? 他们如何获得imageView来显示在角落被截断(即非矩形)的图像? Do you think they created that player card in Photoshop and used it as the background for the imageView image, or did they create it programmatically? 您是否认为他们在Photoshop中创建了该播放器卡并将其用作imageView图像的背景,还是他们以编程方式创建了它?

I wonder because the image is behind the picture of the bear, so I imagine if they created the background in Photoshop, how would they get the image behind the bear head? 我想知道图像是否在熊的照片后面,所以我想如果他们在Photoshop中创建背景,他们将如何获得熊头后面的图像? They can't have just created the card with the player's picture as part of it, then loaded the whole image because if they traded the player, because I'm sure they pull the player info and picture from the web so they can have a card for all players, even if they trade or acquire a new player mid-season, without having to update the app (and add the finished image to images.xcassets ). 他们不能只创建带有玩家照片的卡,然后加载整个图像,因为如果他们交易了玩家,因为我确定他们从网络上提取了玩家信息和图片,这样他们就可以拥有即使所有玩家在赛季中期交易或购买新玩家,也无需更新应用程序(并将完成的图像添加到images.xcassets )即可获得所有用户的images.xcassets

This can be composed from two CALayers at runtime. 这可以在运行时由两个CALayers组成。 Put the picture on the bottom layer; 将图片放在最底层; the picture can come from anywhere - the web, the bundle, etc. the image source could be dynamic. 图片可以来自任何地方-网络,捆绑软件等。图片来源可能是动态的。

Put another CALayer on top, with the frame rendered with opaque colors, and a transparent cut-out for the picture in the middle: 将另一个CALayer放在顶部,用不透明的颜色渲染框架,并在中间放置图片的透明切口:

层数

There are a bunch of ways to do this. 有很多方法可以做到这一点。 A simple and flexible way to do it is to create a CAShapeLayer that's the same size as the image view, with it's origin at 0,0, and add it as the UIImageView's layer's mask. 一种简单而灵活的方法是创建一个与图像视图大小相同的CAShapeLayer,其原点为0,0,并将其添加为UIImageView图层的蒙版。

You'd create a filled UIBezierPath that maps out the part of the image you want to show, and install the bezier path's CGPath into the mask layer's path property. 您将创建一个填充的UIBezierPath,以映射要显示的图像部分,并将贝塞尔曲线路径的CGPath安装到遮罩层的path属性中。

The result would be that the image view is cropped so that only the part inside the shape is drawn. 结果将是裁剪图像视图,以便仅绘制形状内的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM