使用掩蔽从图像裁剪字母

时间:2013-07-24 05:51:07

标签: iphone ios objective-c

我需要什么:

1)从库或相机中选择图像

2)写和文字

3)用图像裁剪文字!

下面的图片可以更清楚地说明我需要什么。

enter image description here

我知道屏蔽和裁剪图像,即使我在emoji me app中使用框架进行遮罩。我只需要知道如何根据动态文本裁剪图像。

请提出您的建议。

...
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;
}

2 个答案:

答案 0 :(得分:5)

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;
}

答案 1 :(得分:2)

这是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];

这将产生以下结果:

enter image description here

你可以搞乱字体的面貌和大小。 CATextLayers可以根据您设置的大小自动换行。您还可以在文本图层上设置文本对齐方式以满足您的需求。

注意: 这里有一点需要注意。如果您需要将输出作为图像,这将无法工作,因为您无法在图层上使用renderInContext:渲染蒙版。此方法仅在您计划在视图中显示时才有效。输出图像需要CoreGraphics方法。