gpt4 book ai didi

ios - 如何创建比设备屏幕大的 UIImage?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:24 27 4
gpt4 key购买 nike

我正在开发一个 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。没有运气。 :(

enter image description here

build phases

enter image description here

进一步编辑: 好的,在上面的 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

最佳答案

单步执行代码。创建 myImage 后,进入控制台并查看 myImage.sizemyImage.scale。将 size 数字乘以 scale

如果您的背景图像是 Retina 质量的,那么您的图像实际上是 1224 x 1224。

来自 the UIImage docs :

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.

如果您的图像实际上是 612 像素(不是)但您的代码将其渲染为 1224 像素,您只需更改比例 属性为 1.0。

如果你的图片实际上是 1224 像素,你需要做一些其他的事情,比如

  • 将您的代码放在支持位图的图形上下文中(即,围绕有问题的代码调用 UIGraphicsBeginImageContext)
  • 向用户显示较小的版本

但是,如果您的图片用于 Instagram,则不应为 1224 x 1224 :-)

更新:我注意到您的应用与俳句相关,所以这里是俳句格式的答案:

Big UIImage?

Bitmap-backed graphics context

Or shrink to 612

关于ios - 如何创建比设备屏幕大的 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14588293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com