【问题标题】:Resizing image gives white image调整图像大小给出白色图像
【发布时间】:2013-03-16 20:33:46
【问题描述】:

我有一个UIImage,在缩放之前我知道它没问题。 我需要对其进行缩放,以便将其设置为 ccsprite 。 我缩放它,但后来我得到了一个白色的图像..

UIImage *scaled=[newImage copy];
//scale the image
CGRect screenRect = CGRectMake(0, 0, 640.0, 960.0);
UIGraphicsBeginImageContext(screenRect.size);
scaled = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CCSprite * image = [CCSprite spriteWithCGImage:scaled.CGImage 
                                           key:[NSString stringWithFormat:@"%d",[localImagesSpritesArray count]]];

会是什么?

【问题讨论】:

  • 您在哪里绘制要缩放到新上下文中的图像? 提示CGContextDrawImage(context, screenRect, scale.CGImage);
  • 为什么要复制newImage然后把结果扔掉?
  • 另外,您不应该对screenRect 进行硬编码。你应该从 UIScreen 获得它。您可能还想使用UIGraphicsBeginImageContextWithOptions 而不是UIGraphicsBeginImageContextWithOptions 版本允许您指定上下文的比例因子,或要求它自动检测。您正在使用的函数无条件使用 1.0 的比例因子,它适用于非 Retina 图像。

标签: ios objective-c uiimage core-graphics


【解决方案1】:
UIGraphicsBeginImageContext(screenRect.size);
[newImage drawInRect:screenRect]; //you have to add this line 
scaled = UIGraphicsGetImageFromCurrentImageContext();

【讨论】:

  • 谢谢,但它会以某种方式改变图像比例并拉伸它..您添加的线条与 screenRect 分辨率之间的关系是什么?他们应该是一样的?
  • @Rant:你告诉它在那个矩形中绘制,所以它会缩放到那个矩形的大小。如果它必须拉伸才能达到那个尺寸,它就会拉伸。如果您想进行方面匹配,请从右侧的“相关”列表中查看此问题:stackoverflow.com/q/1703100/30461
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 2011-10-24
  • 1970-01-01
  • 2011-10-12
  • 2017-12-14
  • 2020-07-16
  • 2013-09-24
相关资源
最近更新 更多