我使用自定义的png图像作为我的.tabBarItem的UITabBarController项目。但是我的png图像太大(64x64 ),所以我使用下面的方法在较小的rect中重新绘制图像(例如,设置size参数(25,25) )。
-(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"];,那么图像就会正确地显示在我的手机上,但是太大了。
我怎样才能解决这个问题?
发布于 2015-10-26 06:58:35
我自己回答这个问题。经过几个小时的调试,问题是:在上面给出的方法中,没有设置CGRect projectRect的CGRect projectRect属性。当我将star.x和unit.y都设置为0之后,一切都完成了。
提示:每次遇到WTF问题时,都要耐心,并尝试以不同的方式测试代码。因为在99.9%的这种情况下,您的代码有问题,而不是Xcode的错误。
虽然我仍然不知道为什么我的问题中的代码在模拟器中工作得很好,但我会放弃它,因为我想有一天当我成为一名专家时,这样的问题既简单又愚蠢。
https://stackoverflow.com/questions/33332940
复制相似问题