我试图以编程方式创建UIImage
并通过CGImageRef mask
应用CGImageCreateWithMask()
来屏蔽- (UIImage *)imageByMaskingHalfImage:(UIImage *)image {
UIGraphicsBeginImageContextWithOptions((CGSize){image.size.width, image.size.height}, NO, image.scale);
[[UIColor blackColor] setFill];
UIRectFill(CGRectMake(0, 0, image.size.width / 2, image.size.height));
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(image.size.width / 2, 0, image.size.width / 2, image.size.height));
UIImage *maskImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef maskImageRef = CGImageMaskCreate(CGImageGetWidth(maskImage.CGImage),
CGImageGetHeight(maskImage.CGImage),
CGImageGetBitsPerComponent(maskImage.CGImage),
CGImageGetBitsPerPixel(maskImage.CGImage),
CGImageGetBytesPerRow(maskImage.CGImage),
CGImageGetDataProvider(maskImage.CGImage),
NULL,
NO);
CGImageRef imageRef = CGImageCreateWithMask(image.CGImage, maskImageRef);
image = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(maskImageRef);
CGImageRelease(imageRef);
return image;
}
。这是我的方法:
maskImage
预览image
时,我可以看到它正确呈现:图像的一半是黑色,一半是白色。但是,生成的蒙版 alertDialog.show();
try {
TextView textView = (TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message);
textView.setTextIsSelectable(true);
}
catch(Exception e) {
// Oups!
}
保持不变,整个图像都可见。
我已阅读文档但无法找到问题。我的代码中肯定存在一些愚蠢的错误,或者我误解了某些内容。
答案 0 :(得分:0)
这就是我在形状A中掩盖图像的方式
创建一个获取蒙版形状的方法,在我的情况下为形状A:
DestroyHandle()
上述方法在名为SHapeLayer的NSObject文件中声明。
并在视图控制器中调用此方法:
-(void)shapeA:(CGRect)frame :(UIImageView*) testView {
//// Drawing
UIBezierPath* textPath = UIBezierPath.bezierPath;
[textPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 284.92, CGRectGetMinY(frame) + 243.03)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 230.48, CGRectGetMinY(frame) + 84.58)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 172.62, CGRectGetMinY(frame) + 243.03)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 284.92, CGRectGetMinY(frame) + 243.03)];
[textPath closePath];
[textPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 205.09, CGRectGetMinY(frame) + 31.36)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 260.02, CGRectGetMinY(frame) + 31.36)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 390.14, CGRectGetMinY(frame) + 390)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 336.92, CGRectGetMinY(frame) + 390)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 300.54, CGRectGetMinY(frame) + 282.58)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 158.7, CGRectGetMinY(frame) + 282.58)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 119.88, CGRectGetMinY(frame) + 390)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 70.08, CGRectGetMinY(frame) + 390)];
[textPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 205.09, CGRectGetMinY(frame) + 31.36)];
[textPath closePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = textPath.CGPath;
shapeLayer.strokeColor = [UIColor clearColor].CGColor;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
[testView.layer setMask:shapeLayer];
}
现在在加载图片之前调用此方法-(void)callShape{
CGRect temp=self.imageView.bounds;
ShapeLayer *sl=[[ShapeLayer alloc]init];
[sl shapeA:temp :self.imageView];
}
。