iphone - 如何写UIImage?

标签 iphone ios uiimage quartz-2d

该代码无法正常工作,我不理解该错误。
请帮忙。

   -(UIImage *)addText:(UIImage *)img text:(NSString *)text1
    {    
        int w = img.size.width;
        int h = img.size.height; 
        //lon = h - lon;
        char* text  = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        /*UIGraphicsBeginImageContext(self.view.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        CGContextShowTextAtPoint(UIGraphicsGetCurrentContext(), 50, 100, text, strlen(text));
        UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSLog(@"View Image : %@",viewImage);*/

        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

        //char* text    = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];// "05/05/09";
        CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(context, kCGTextFill);
        CGContextSetRGBFillColor(context, 255, 255, 255, 1);

        //rotate text
        CGContextShowTextAtPoint(context, 0, 50, text, strlen(text));
        CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
        CGContextShowTextAtPoint(context, 50, 100, text, strlen(text));
        //UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext(); 
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        NSLog(@"Image : %@",[UIImage imageWithCGImage:imageMasked]);

        return [UIImage imageWithCGImage:imageMasked];   
        //return viewImage;
    }

最佳答案

除非您有理由避免添加NSString UIKit,否则就足够了:

+(UIImage*) drawText:(NSString*) text 
             inImage:(UIImage*)  image 
             atPoint:(CGPoint)   point
                font:(UIFont*)   font
               color:(UIColor*)  color
{
    UIGraphicsBeginImageContextWithOptions(image.size, FALSE, 0.0);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [color set];
    [text drawInRect:CGRectIntegral(rect) withFont:font]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();

    return newImage;
}

关于iphone - 如何写UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345786/

相关文章:

iphone - 两部 iPhone 之间的距离

iphone - iOS 自动布局 : Resizing container with constraints

ios - 为什么粘贴长字符串时带有安全文本输入的文本字段会崩溃 (iOS10)

ios - 先执行代码! swift

ios - 将所有 UILabels(具有正确的来源)合并到 UIImage 中

iphone - 在 UIPopover 中显示 ABPeoplePickerNavigationController

iphone - Xcode 4.3.1 将文件导入新项目

ios - SwiftUI Firestore 查询游标分页不起作用

ios - 将数组中的 UI 图像快速附加到幻灯片

macos - 了解两个图像是否相同的最有效方法?