Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何写入UIImage?_Iphone_Ios_Uiimage_Quartz 2d - Fatal编程技术网

Iphone 如何写入UIImage?

Iphone 如何写入UIImage?,iphone,ios,uiimage,quartz-2d,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:NSASCII

这个代码不起作用,我不理解这个错误。 请帮忙

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

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

我会建议更好的格式,简化和更好的解释它应该做什么。我认为代码对它应该做什么是不言自明的。我会建议更好的格式,简化和更好地解释它应该做什么。我认为代码对它应该做什么是不言自明的。耶字符串肯定不是空的。也许你传递的图像太小,你无法看到结果。您的代码确实有效。耶字符串肯定不是空的。可能您传递的图像太小,您无法看到结果。你的代码确实有效。