【问题标题】:How to write to UIImage?如何写入 UIImage?
【发布时间】:2011-09-08 09:19:01
【问题描述】:

此代码不起作用,我不明白这个错误。 请帮忙。

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

【问题讨论】:

  • 我建议更好地格式化、简化并更好地解释它应该做什么。
  • 我认为代码是不言自明的。

标签: iphone ios uiimage quartz-2d


【解决方案1】:

除非您有理由避免添加 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;
}

【讨论】:

    【解决方案2】:

    您的代码确实有效。

    确保传入的字符串不为空。

    【讨论】:

    • 是的,字符串肯定不是空的。
    • 可能你传入的图片太小,看不到结果。您的代码确实有效。
    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2012-01-09
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多