从图像使用Masking裁剪字母
我需要的:
1)从库或照相机中select图像
2)写和文字
3)文字是裁剪与图像!
下面的图片可以澄清更多我究竟需要什么。
我知道掩蔽和图像的裁剪,即使我用emoji我的应用程序框架掩盖。 只是我需要知道如何根据dynamic文本裁剪图像。
请给出你的build议。
... UIImage *image = [UIImage imageNamed:@"dogs.png"]; UIImage *mask = [UIImage imageNamed:@"mask.png"]; // result of the masking method UIImage *maskedImage = [self maskImage:image withMask:mask]; ... - (UIImage*) maskImage:(UIImage *)image 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 maskedImageRef = CGImageCreateWithMask([image CGImage], mask); UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef]; CGImageRelease(mask); CGImageRelease(maskedImageRef); // returns new image with mask applied return maskedImage; }
1)用下面的代码写一个带有文本的图像
UIImage *textedImage = [self imageFromText:@"Text to show"]; -(UIImage *)imageFromText:(NSString *)text { //set width for string to wrap. CGSize maximumSize; maximumSize = CGSizeMake(320, 300); //set your text image font here UIFont *font = [UIFont boldSystemFontOfSize:50]; CGSize strSize1 = [text sizeWithFont:font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap]; CGSize strSize =CGSizeMake(320, strSize1.height); if (UIGraphicsBeginImageContextWithOptions != NULL) UIGraphicsBeginImageContextWithOptions(strSize,NO,0.0); else UIGraphicsBeginImageContext(strSize); //set your new text iamge frame here CGRect newframe = CGRectMake(0, 0, 320, 400); UIColor *color = [UIColor redColor]; [color set]; [text drawInRect:newframe withFont:font lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentCenter]; UIImage *textImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return textImg; }
2)获得纹理图像后,将图像制作成所需的图像
UIImage *maskedImage = [self maskImage:finalImage withMask: textedImage]; - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage { CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //UIImage *maskImage = [UIImage imageNamed:@"mask.png"]; CGImageRef maskImageRef = [maskImage CGImage]; // create a bitmap graphics context the size of the image CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast); if (mainViewContentContext==NULL) return NULL; CGFloat ratio = 0; ratio = maskImage.size.width/ image.size.width; if(ratio * image.size.height < maskImage.size.height) { ratio = maskImage.size.height/ image.size.height; } CGRect rect1 = {{0, 0}, {maskImage.size.width, maskImage.size.height}}; CGRect rect2 = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}}; CGContextClipToMask(mainViewContentContext, rect1, maskImageRef); CGContextDrawImage(mainViewContentContext, rect2, image.CGImage); // Create CGImageRef of the main view bitmap content, and then // release that bitmap context CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext); CGContextRelease(mainViewContentContext); UIImage *theImage = [UIImage imageWithCGImage:newImage]; CGImageRelease(newImage); // return the image return theImage; }
这是一个CoreAnimation方法。 您可以使用CATextLayer
来使用图像遮罩图层。 这是一个更紧凑的代码明智的:
// Create our text layer we want to use as a mask CATextLayer *textLayer = [CATextLayer layer]; [textLayer setBounds:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)]; [textLayer setFont:@"Helvetica-Bold"]; [textLayer setFontSize:42.0f]; [textLayer setAnchorPoint:CGPointMake(0.0f, 0.0f)]; [textLayer setPosition:CGPointMake(0.0f, 0.0f)]; [textLayer setWrapped:YES]; [textLayer setAlignmentMode:kCAAlignmentCenter]; [textLayer setString:@"Hello World And Something Something!!"]; // Create our image layer CALayer *imageLayer = [CALayer layer]; [imageLayer setBounds:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)]; [imageLayer setPosition:[[self view] center]]; [imageLayer setContents:(id)[[UIImage imageNamed:@"pp"] CGImage]]; // Set the mask [imageLayer setMask:textLayer]; // Add the layer to the view's layer tree [[[self view] layer] addSublayer:imageLayer];
这将产生以下结果:
你可以混乱的字体和大小。 CATextLayers可以根据您设置的大小自动换行。 您还可以在文本图层上设置文本alignment以满足您的需求。
注意:一个警告在这里。 如果您需要输出作为图像,这将无法正常工作,因为您无法在图层上使用renderInContext:渲染蒙版。 此方法只适用于您计划只显示在视图中。 输出图像将需要CoreGraphics方法。
- 如何获取包含年份的月份名称和两个日期之间的年份列表
- 我如何自学iOS 11,Swift 4和Metal2 API,ARKit,并在2Weeks中制作了原型。
- Textview中心文本alignmentIOS 7
- @IBDesignable视图不会在Interface Builder中绘制背景颜色
- 如何在不使用故事板的情况下创建通知内容扩展
- 在iPhone上的耳机和扬声器之间切换
- Facebook iOS SDK没有调用完成处理程序
- iOS黑屏和MissingMethodException:未find方法:'System.Type.op_Equality'
- 使用Swift将当前时间设置为yyyy-MM-dd 00:00:00