我实际上要做的是在视图中有一个文本标签"切割"一个文本形状的孔.我试过使用self.mask = uiLabel
但是那些拒绝正确定位文本所以我通过Core Graphics接近这个.
这是不起作用的代码(在draw(_ rect: CGRect)
)中:
let context = (UIGraphicsGetCurrentContext())!
// Set mask background color
context.setFillColor(UIColor.black.cgColor)
context.fill(rect)
context.saveGState()
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [
NSParagraphStyleAttributeName: paragraphStyle,
NSFontAttributeName: UIFont.systemFont(ofSize: 16, weight: UIFontWeightMedium),
NSForegroundColorAttributeName: UIColor.white
]
let string = NSString(string: "LOGIN")
// This wouldn't vertically align so we calculate the string size and create a new rect in which it is vertically aligned
let size = string.size(attributes: attributes)
let position = CGRect( …
Run Code Online (Sandbox Code Playgroud) 在我的一个iOS应用程序中,我正在尝试使用切割图像的一部分CGImageMask
.我已成功使用以下代码屏蔽图像:
- (UIImage *)maskImage:(UIImage *)referenceImage withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([referenceImage CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
Run Code Online (Sandbox Code Playgroud)
所以,我的形象将是:
myImageView.image = [self maskImage:[UIImage imageNamed:@"image.png"]
withMask:[UIImage imageNamed:@"mask.png"]];
Run Code Online (Sandbox Code Playgroud)
问题: 输出图像与参考图像('image.png')的大小相同,周围有透明区域.但我想避开那些透明区域,并裁剪结果图像.我怎样才能做到这一点?有几个掩模,并且掩模框架与所有掩模框架不相似.我在这里附上问题概述的参考图像.请帮帮我的朋友.提前致谢.
我有这个代码:
Texture2D *cachedTexture;
if(cachedTexture = [cachedTextures objectForKey:aName]) {
return cachedTexture;
}
// We are using imageWithContentsOfFile rather than imageNamed, as imageNamed caches the image in the device.
// This can lead to memory issue as we do not have direct control over when it would be released. Not using
// imageNamed means that it is not cached by the OS and we have control over when it is released.
NSString *filename = [aName stringByDeletingPathExtension];
NSString *filetype = [aName pathExtension];
NSString …
Run Code Online (Sandbox Code Playgroud)