首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >png图像不显示在"reDraw“之后。

png图像不显示在"reDraw“之后。
EN

Stack Overflow用户
提问于 2015-10-25 17:50:34
回答 1查看 34关注 0票数 0

我使用自定义的png图像作为我的.tabBarItem的UITabBarController项目。但是我的png图像太大(64x64 ),所以我使用下面的方法在较小的rect中重新绘制图像(例如,设置size参数(25,25) )。

代码语言:javascript
复制
-(UIImage*) getSmallImage:(UIImage*)image inSize: (CGSize)size
{
    CGSize originalImageSize = image.size;

    CGRect newRect = CGRectMake(0, 0, size.width, size.height);

    float ratio = MAX(newRect.size.width/originalImageSize.width,
                      newRect.size.height/originalImageSize.height);

    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];

    [path addClip];

    CGRect projectRect;
    projectRect.size.width = ratio * originalImageSize.width;
    projectRect.size.height = ratio * originalImageSize.height;

    //projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
    //projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;
    // Draw the image on it
    [image drawInRect:projectRect];
    // Get the image from the image context
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();

    // Cleanup image context resources
    UIGraphicsEndImageContext();

    return smallImage;
}

我使用的每一张图像都由此方法返回。在模拟器上一切都很好,但当我在iphone上测试这些图像时,它们并没有显示出来。但是如果我放弃上面的方法,像这样直接导入图像:self.tabBarItem.image = [UIImage imageNamed:@"Input"];,那么图像就会正确地显示在我的手机上,但是太大了。

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-26 06:58:35

我自己回答这个问题。经过几个小时的调试,问题是:在上面给出的方法中,没有设置CGRect projectRectCGRect projectRect属性。当我将star.x和unit.y都设置为0之后,一切都完成了。

提示:每次遇到WTF问题时,都要耐心,并尝试以不同的方式测试代码。因为在99.9%的这种情况下,您的代码有问题,而不是Xcode的错误。

虽然我仍然不知道为什么我的问题中的代码在模拟器中工作得很好,但我会放弃它,因为我想有一天当我成为一名专家时,这样的问题既简单又愚蠢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33332940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档