gpt4 book ai didi

iphone - 使用蒙版从图像中裁剪字母

转载 作者:可可西里 更新时间:2023-11-01 17:08:42 25 4
gpt4 key购买 nike

我需要什么:

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

2)写和发短信

3) 文本被图片裁剪了!

下图可以更清楚地说明我到底需要什么。

enter image description here

即使我在 emoji me app 中使用框架进行 mask ,我也知道图像的 mask 和裁剪.只是我需要知道如何根据动态文本裁剪图像。

请提出您的建议。

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

关于iphone - 使用蒙版从图像中裁剪字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17826160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com