圖像添加文本我想打一個iPhone應用程序類似,一些賀卡應用,在那裏我可以寫文字了一些預先準備好的背景圖片(卡)。超過上的Xcode
- 怎麼能在我寫這篇文字?
- 如何保存的背景圖像+上一個圖像文件中的文本?
感謝。
圖像添加文本我想打一個iPhone應用程序類似,一些賀卡應用,在那裏我可以寫文字了一些預先準備好的背景圖片(卡)。超過上的Xcode
感謝。
下面是燃燒字符串轉換成圖像的方法。您可以調整字體大小和其他參數以根據自己的喜好進行配置。
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ([text length] > 200) {
fontSize = 10;
}
UIFont *font = [UIFont boldSystemFontOfSize: fontSize]; // set text font
[ text drawInRect : aRectangle // render the text
withFont : font
lineBreakMode : UILineBreakModeTailTruncation // clip overflow from end of last line
alignment : UITextAlignmentCenter ];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
謝謝Rayfleck!有效。
要添加具有視網膜顯示器可選兼容性(在 '@ 2X' 版本圖像的修正波濤洶涌字母擴展):
取代:
UIGraphicsBeginImageContext(img.size);
帶有條件:
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(img.size,NO,0.0);
else
UIGraphicsBeginImageContext(img.size);
更新ios7 ...
/* Creates an image with a home-grown graphics context, burns the supplied string into it. */
- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img {
UIGraphicsBeginImageContext(img.size);
CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height);
[img drawInRect:aRectangle];
[[UIColor redColor] set]; // set text color
NSInteger fontSize = 14;
if ([text length] > 200) {
fontSize = 10;
}
UIFont *font = [UIFont fontWithName:@"Courier" size:fontSize];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;
NSDictionary *attributes = @{ NSFontAttributeName: font,
NSParagraphStyleAttributeName: paragraphStyle,
NSForegroundColorAttributeName: [UIColor whiteColor]};
[text drawInRect:aRectangle withAttributes:attributes];
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); // extract the image
UIGraphicsEndImageContext(); // clean up the context.
return theImage;
}
這種方法考慮到了屏幕的尺寸,它的超級直覺
UIImage * img = ...
UIImageView * iV = [[UIImageView alloc] initWithImage:img];
UILabel * l = [[UILabel alloc] initWithFrame:iV.bounds];
l.textAlignment = ...;
l.adjustsFontSizeToFitWidth = YES;
l.textColor = ...;
l.font = ...;
l.text = ...;
[iV addSubview:l];
UIGraphicsBeginImageContextWithOptions(iV.bounds.size, NO, 0);
[iV.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage
核心圖形可能是正確的方向。 – dasdom
你可以製作一個隱藏狀態欄的空白控制器,並在完成編輯後製作截圖。我認爲UIImagePNGRepresentation是你需要的。 –
感謝您的回答。是的,我認爲你的解決方案比使用Core Graphics更容易,我會專注於此。如果我明白了,你的意思是我在一個UIView準備的背景圖像和透明bachground就加一的UITextField,讓用戶輸入文本然後進行scereenshot。這可能是嗎? – hafedh