【发布时间】:2013-01-29 17:09:06
【问题描述】:
我正在开发一款 iPhone 应用程序,该应用程序可以创建图片并将其发布到 Facebook 和 Instagram。
Facebook 照片的正确尺寸似乎是 350x350,实际上这段代码完全按照我的意愿创建了一个 350x350 的图像:
-(UIImage *)createImage {
UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenHeight/2-349, 349, 349)];
v.image = [UIImage imageNamed:@"backgroundForFacebook.png"]; //"backgroundForFacebook.png" is 349x349.
//This code adds some text to the image.
CGSize dimensions = CGSizeMake(screenWidth, screenHeight);
CGSize imageSize = [self.ghhaiku.text sizeWithFont:[UIFont fontWithName:@"Georgia"
size:mediumFontSize]
constrainedToSize:dimensions lineBreakMode:0];
int textHeight = imageSize.height+16;
UITextView *tv = [self createTextViewForDisplay:self.ghhaiku.text];
tv.frame = CGRectMake((screenWidth/2)-(self.textWidth/2),s creenHeight/3.5,
self.textWidth/2 + screenWidth/2, textHeight*2);
[v addSubview:tv];
//End of text-adding code
CGRect newRect = CGRectMake(0, screenHeight/2-349, 349, 349);
UIGraphicsBeginImageContext(newRect.size);
[[v layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[v removeFromSuperview];
return myImage;
}
但是当我使用相同的代码创建一个需要为 612x612 的 Instagram 图片时,我只得到文本,没有背景图片:
-(UIImage *)createImageForInstagram {
UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 612, 612)];
v.image = [UIImage imageNamed:@"backgroundForInstagram.png"]; //"backgroundForInstagram.png" is 612x612.
//...text-adding code...
CGRect newRect = CGRectMake(0, 0, 612, 612);
UIGraphicsBeginImageContext(newRect.size);
[[v layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[v removeFromSuperview];
return myImage;
}
我做错了什么,我该如何解决?
(虽然我在这里,但我还要说我对使用图形上下文很陌生,所以如果代码中有任何尴尬之处,我会很感激你指出。)
编辑:现在我已将这两种方法简化为一种,这一次我什至没有得到文本。啊!
-(UIImage *)addTextToImage:(UIImage *)myImage withFontSize:(int)sz {
NSString *string=self.displayHaikuTextView.text;
NSString *myWatermarkText = [string stringByAppendingString:@"\n\n\t--haiku.com"];
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Georgia"
size:sz],
NSFontAttributeName,
nil];
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:myWatermarkText attributes:attrs];
UIGraphicsBeginImageContextWithOptions(myImage.size,NO,1.0);
[myImage drawAtPoint: CGPointZero];
NSString *longestLine = ghv.listOfLines[1];
CGSize sizeOfLongestLine = [longestLine sizeWithFont:[UIFont fontWithName:@"Georgia" size:sz]];
CGSize siz = CGSizeMake(sizeOfLongestLine.width, sizeOfLongestLine.height*5);
[attString drawAtPoint: CGPointMake(myImage.size.width/2 - siz.width/2, myImage.size.height/2-siz.height/2)];
myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return myImage;
}
当我传递参数 [UIImage imageNamed:"backgroundForFacebook.png"](图像 349x349)和 12 时,一切都很好。我明白了。当我传递参数 [UIImage imageNamed:"backgroundForInstagram.png"](图像 612x612)和 24 时,什么也没做。
现在我只是将文本放在较小的图像 (@"backgroundForFacebook.png") 上,然后调整它的大小,但这会使文本变得模糊,这是我不喜欢的。
编辑: 只是为了涵盖基础知识,这里是 1)我调用此方法的方法(检查拼写)和 2)支持文件和构建阶段的图像(到显示图像实际上在那里)。我还尝试分配longestLine 一个非变量NSString。没有运气。 :(
进一步编辑:好的,在上面addTextToImage: 期间记录图像的大小和比例,这是我得到的较小图像的结果,即有效的图像:
2013-02-04 22:24:09.588 GayHaikuTabbed[38144:c07] 349.000000, 349.000000, 1.000000
这就是我从大图得到的结果——太棒了。
Feb 4 22:20:36 Joels-MacBook-Air.local GayHaikuTabbed[38007] <Error>: CGContextGetFontRenderingStyle: invalid context 0x0
Feb 4 22:20:36 Joels-MacBook-Air.local GayHaikuTabbed[38007] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
//About thirty more of these.
Feb 4 22:20:36 Joels-MacBook-Air.local GayHaikuTabbed[38007] <Error>: CGBitmapContextCreate: unsupported parameter combination: 0 integer bits/component; 0 bits/pixel; 0-component color space; kCGImageAlphaNoneSkipLast; 2448 bytes/row.
Feb 4 22:20:36 Joels-MacBook-Air.local GayHaikuTabbed[38007] <Error>: CGContextDrawImage: invalid context 0x0
Feb 4 22:20:36 Joels-MacBook-Air.local GayHaikuTabbed[38007] <Error>: CGBitmapContextCreateImage: invalid context 0x0
【问题讨论】:
-
您是否认真设置了
CALayer层次结构,然后将其渲染到上下文中以创建图像?这太疯狂了。只需使用 CoreGraphics API。 -
我相信这就是我在添加的编辑代码中所做的吗?我不确定——正如我所说,我对图形上下文很陌生......或者我根本不需要上下文?
-
(也就是说,我不会反对“疯狂”的标签。:))
-
是的,您编辑的版本更加理智。
-
顺便说一句,如果您只是使用给定字体绘制字符串,则不需要
NSAttributedString,您可以使用NSString绘图例程(这是您用来已经获得尺寸)。
标签: ios graphics uiimage instagram