- (UIImage *)imageWithLogoText:(UIImage *)img text:( NSString *)text1 { /////注:此爲後來更改,用於顯示中文。zyq,2013-5-8 CGSize size = CGSizeMake(200, img.size.height); //設置上下文(畫布)大小 UIGraphicsBeginImageContext(size); //建立一個基於位圖的上下文(context),並將其設置爲當前上下文 CGContextRef contextRef = UIGraphicsGetCurrentContext(); //獲取當前上下文 CGContextTranslateCTM(contextRef, 0, img.size.height); //畫布的高度 CGContextScaleCTM(contextRef, 1.0, -1.0); //畫布翻轉 CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), [img CGImage]); //在上下文種畫當前圖片 [[UIColor redColor] set]; //上下文種的文字屬性 CGContextTranslateCTM(contextRef, 0, img.size.height); CGContextScaleCTM(contextRef, 1.0, -1.0); UIFont *font = [UIFont boldSystemFontOfSize:16]; [text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font]; //此處設置文字顯示的位置 UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext(); //從當前上下文種獲取圖片 UIGraphicsEndImageContext(); //移除棧頂的基於當前位圖的圖形上下文。 return targetimg; //注:此爲原來,不能顯示中文。無用。 //get image width and height int w = img.size.width; int h = img.size.height; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); //create a graphic context with CGBitmapContextCreate CGContextRef context = CGBitmapContextCreate( NULL , w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1); char * text = ( char *)[text1 cStringUsingEncoding: NSUnicodeStringEncoding ]; CGContextSelectFont(context, "Georgia" , 30, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextFill); CGContextSetRGBFillColor(context, 255, 0, 0, 1); CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text)); //Create image ref from the context CGImageRef imageMasked = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); return [UIImage imageWithCGImage:imageMasked]; } |