Ios 将uiview捕获到uiimage时,遮罩不起作用
我有一个Ios 将uiview捕获到uiimage时,遮罩不起作用,ios,uiview,uiimage,calayer,mask,Ios,Uiview,Uiimage,Calayer,Mask,我有一个ui视图,我想将其保存为ui图像。我使用UIGraphicsBeginImageContext来实现这一点,它工作正常。但是,当我对(view/layer.mask)中的图像应用掩码时,我通过UIGraphicsBeginImageContext捕获的图像是错误的(在运行应用程序时,掩码起作用,但在尝试从UIView获取UIImage时,掩码不起作用)。有人遇到过类似的问题吗?如果我理解正确,您希望从UIView层创建UIImage,而该层被屏蔽。我假设您希望目标图像具有透明背景 我在实
ui视图
,我想将其保存为ui图像
。我使用UIGraphicsBeginImageContext
来实现这一点,它工作正常。但是,当我对(view/layer.mask)中的图像应用掩码时,我通过UIGraphicsBeginImageContext
捕获的图像是错误的(在运行应用程序时,掩码起作用,但在尝试从UIView
获取UIImage
时,掩码不起作用)。有人遇到过类似的问题吗?如果我理解正确,您希望从UIView层创建UIImage,而该层被屏蔽。我假设您希望目标图像具有透明背景
我在实现这一点时没有遇到任何问题,我有一个演示项目,您可以查看:
您首先需要按下遮罩按钮。它将从包中加载一个遮罩图像(黑色和白色),并将其设置为上面UIView容器的层遮罩
然后,您可以按下复制图像按钮,将UIView容器渲染为UIImage,然后将其设置为下面的目标图像视图以查看结果
我还将在这里发布这两种方法:
- (IBAction)onMask:(id)sender {
UIImage* maskImage = [UIImage imageNamed:@"star.png"];
UIImageView* maskImageView = [[UIImageView alloc] initWithImage:maskImage];
maskImageView.contentMode = UIViewContentModeScaleAspectFit;
maskImageView.frame = _mainContainerView.bounds;
_mainContainerView.layer.mask = maskImageView.layer;
}
- (IBAction)onCopyImage:(id)sender {
UIGraphicsBeginImageContextWithOptions(_mainContainerView.bounds.size, FALSE, [[UIScreen mainScreen] scale]);
[_mainContainerView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_destImageView.image = img;
}
编辑
显然,IOS6上的renderInContext:
没有使用掩码(这里也这么说)。
我的解决方案是手动将遮罩应用于图像。遮罩取自图层的遮罩属性,并在上下文中渲染,因此我们在转换/contentModes/等方面没有任何问题
以下是更新的源代码(也可在bitbucket上获得):
编辑
请检查bitbucket上的最新项目,因为它包含最新版本 下面是一些示例代码,我使用这些代码将UIView获取为UIImageView,并为此应用掩码图像
* UIView --> UIImageView --> With Mask images .
我已经使用UIView进行绘图,因此
将UIView(绘图区域)指定给UIImageView。
-(UIImage*) imageRepresentation
{
[EAGLContext setCurrentContext:context];
UIImageView* upsideDownImageView=[[UIImageView alloc] initWithImage: [self upsideDownImageRepresenation]];
upsideDownImageView.transform=CGAffineTransformScale(upsideDownImageView.transform, 1, -1);
UIView* container=[[UIView alloc] initWithFrame:upsideDownImageView.frame];
[container addSubview:upsideDownImageView];
UIImage* toReturn=nil;
UIGraphicsBeginImageContext(container.frame.size);
[container.layer renderInContext:UIGraphicsGetCurrentContext()];
toReturn = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[upsideDownImageView release];
upsideDownImageView = nil;
container = nil;
//[container release];
return toReturn;
}
现在您有了UIView的UIImageView表示。
-(UIImage*) imageRepresentation
{
[EAGLContext setCurrentContext:context];
UIImageView* upsideDownImageView=[[UIImageView alloc] initWithImage: [self upsideDownImageRepresenation]];
upsideDownImageView.transform=CGAffineTransformScale(upsideDownImageView.transform, 1, -1);
UIView* container=[[UIView alloc] initWithFrame:upsideDownImageView.frame];
[container addSubview:upsideDownImageView];
UIImage* toReturn=nil;
UIGraphicsBeginImageContext(container.frame.size);
[container.layer renderInContext:UIGraphicsGetCurrentContext()];
toReturn = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[upsideDownImageView release];
upsideDownImageView = nil;
container = nil;
//[container release];
return toReturn;
}
现在让我们用遮罩效果来制作这个
你能提供一些代码来展示你目前所拥有的吗?你是否在捕获UIImage之前屏蔽了源UIView?或者,在试图在UIImageView中显示生成的UIImage时,您是否屏蔽了它?我添加了一些示例代码。没有用于屏蔽的示例编码:
UIImage*\u maskingImage=self.maskImage;CALayer*_maskingLayer=[CALayer layer]_maskingLayer.frame=self.bounds;[[u maskingLayer setContents:(id)[[u masking image CGImage]];[self.layer setMask:_maskingLayer]代码>这对我不起作用。我仍然在UIView的图像中得到平方。我使用和你完全相同的代码。我不明白???你的示例项目对我有用。可能是iOS 7 SDK的问题。您的项目是在iOS 7中构建的,而我的项目是在iOS 6中构建的。这会有什么不同吗?我会尝试使用ios 6,并给出一个答案。事实上,ios 6.1不起作用,我没想到会出现这种情况。我将介绍如何更新项目以使其正常工作。我添加了一个新的解决方案,该解决方案使用更少的内存,但掩码图像被反转(因为CGImageCreateWithMask
的工作方式)。很抱歉,在我的项目中,我有多个屏蔽UIView。你有什么解决办法吗?
-(UIImage*) upsideDownImageRepresenation
{
int imageWidth = CGRectGetWidth([self bounds]);
int imageHeight = CGRectGetHeight([self bounds]);
//image buffer for export
NSInteger myDataLength = imageWidth* imageHeight * 4;
// allocate array and read pixels into it.
GLubyte *tempImagebuffer = (GLubyte *) malloc(myDataLength);
glReadPixels( 0, 0, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, tempImagebuffer);
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, tempImagebuffer, myDataLength, ProviderReleaseData);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * imageWidth;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef] ;
CGDataProviderRelease(provider);
CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
return myImage;
}
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0); //imageView is wt we got by converting the UIIVew.
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
// Add the mask Images even if want any UILabels etc ..
[imageView.image drawInRect:CGRectMake(0, 0, 703, 315)];
[maskImages.image drawAtPoint:CGPointMake(10, 10) blendMode:kCGBlendModeNormal alpha:0.2];
[lblAckNo drawTextInRect:CGRectMake(320, 230,100,50)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();// image with all of your effort.
[[UIColor redColor] set];
UIGraphicsEndImageContext();