Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Ios 如何将UIEDGEINSET应用于CGImage?_Ios_Swift_Cgimage - Fatal编程技术网

Ios 如何将UIEDGEINSET应用于CGImage?

Ios 如何将UIEDGEINSET应用于CGImage?,ios,swift,cgimage,Ios,Swift,Cgimage,如何将UIEdgeInsets(或其一些变体)应用于CGImage?这就是我最终想要实现的,但是CGImage在转换为CGImage之前,无法识别应用于UIImage的底部插图 var image = someUIImage image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: (image.size.height / 2), right: 0)) let imageCG: CGImage = im

如何将
UIEdgeInsets
(或其一些变体)应用于
CGImage
?这就是我最终想要实现的,但是
CGImage
在转换为
CGImage
之前,无法识别应用于
UIImage
的底部插图

var image = someUIImage
image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: (image.size.height / 2), right: 0))
let imageCG: CGImage = image.cgImage!
layer.contents = imageCG

我不能直接将插入应用于
CGImage
(如图所示)

解决方法?

请尝试以下方法:


设resizeble=img.resizebleimage(带capinsets:UIEdgeInsets(top:8,left:8,bottom:8,right:8),resizengmode:.stretch)

您不能对
CGImage
应用
UIEdgeInsets
,因为
CGImage
没有
对齐rectinests
属性。
UIImage
存储了
CGImage
没有的几个属性,包括
alignmentrectinets

请注意,
alignmentRectInsets
属性的存在会影响自动布局如何构成包含图像的
UIImageView
UIImageView
从其
UIImage
alignmentrectinests
设置自己的
alignmentrectinests
。然后,自动布局在计算
UIImageView
的帧时使用对齐矩形

自动布局不适用于层,因此
CALayer
不需要
alignmentrectinests
属性

换句话说,你所要求的并没有真正意义


如果你修改你的问题来解释你想要什么样的视觉效果,我可能会提供如何获得它的建议。

不幸的是,插图仍然没有延续到
CGImage
。解释得很好:)我不知道UIImageView从UIImage获取alignmentRectInsets!!!因此+1。谢谢你的深入回答,我觉得它很有帮助。我想要的视觉效果只是将底部边距应用于
CGImage
。如果可以,我会使用标准的
UIImage
,但此图像将是地图上的注释,必须是
CGImage
类型。您能在
UIView
或其某个子类中正确显示内容吗?我能,但不幸的是
CGImage
正是此特定委托方法所需要的(该图像被用作地图上的注释)因此我几乎没有回旋余地。今天下午我实际上正在编写一些代码。我有代码将任何
UIView
及其子视图转换为
UIImage
,效果非常好。现在我需要调整/裁剪/缩放该图像(实际上它是一个遮罩)为了反映一个特定的大小。嗯,一旦你有了一个
UIImge
,把它改成
CGImage
(或者反过来,这就是我现在正在做的)就是一行代码。你认为这对你有用吗?(1)把所有的东西,包括插图,都放在一个视图中。(2)创建一个适当大小的视图缩放图像。如果是这样,当我完成后(希望今天),我会发布完整的代码。
let image = someUIImage.cgImage!
image. // can't apply any inset here
layer.contents = image