UIImage imageNamed:始终以1x返回图像 [英] UIImage imageNamed: always returns image at 1x

查看:171
本文介绍了UIImage imageNamed:始终以1x返回图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self baseInit];
    }
    return self;
}

- (void)baseInit {
    _selectedImage = [UIImage imageNamed:@"SelectedStar"];
    _deselectedImage = [UIImage imageNamed:@"DeselectedStar"];
    _rating = 6.5f;
    _editable = NO;
    _imageViews = [[NSMutableArray alloc] init];
    _maxRating = 10;
    _minImageSize = CGSizeMake(5, 5);
    _delegate = nil;

    [self setBackgroundColor:[UIColor whiteColor]];
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"Star size: %@", NSStringFromCGSize(_selectedImage.size));
    NSLog(@"Star scale: %f", _selectedImage.scale);      
    ...
}

现在,无论我使用哪种目标操作系统或设备模拟器,NSLog都可以为我提供:

2015-01-31 16:54:36.709 RatingViewDeveloper[6076:1679432] Star size: {21.5, 20}
2015-01-31 16:54:36.710 RatingViewDeveloper[6076:1679432] Star scale: 2.000000

我的images.xcassets中的文件如下:

    名为DeselectedStar
  • ImageSet带有图像:
    • DeselectedStar.png
    • DeselectedStar@2x.png
    • DeselectedStar@3x.png

,对于名为SelectedStar的ImageSet也是一样.

它返回的大小始终是标准大小图像的大小,而不是2x甚至3x版本,我不明白为什么,毕竟毕竟是2.0.我尝试再次清洁和构建,但结果始终是糟糕的1x图像.

解决方案

图像的高度和宽度始终为1x,2x会自动调整,分辨率会加倍,但高度和宽度不会改变.

重命名并将其他图像设置为DeselectedStar@2x.png,您将看到我在说什么.

This is my code:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self baseInit];
    }
    return self;
}

- (void)baseInit {
    _selectedImage = [UIImage imageNamed:@"SelectedStar"];
    _deselectedImage = [UIImage imageNamed:@"DeselectedStar"];
    _rating = 6.5f;
    _editable = NO;
    _imageViews = [[NSMutableArray alloc] init];
    _maxRating = 10;
    _minImageSize = CGSizeMake(5, 5);
    _delegate = nil;

    [self setBackgroundColor:[UIColor whiteColor]];
}

- (void)drawRect:(CGRect)rect {
    NSLog(@"Star size: %@", NSStringFromCGSize(_selectedImage.size));
    NSLog(@"Star scale: %f", _selectedImage.scale);      
    ...
}

Now no matter what target OS or device simulator I use, the NSLog gives me:

2015-01-31 16:54:36.709 RatingViewDeveloper[6076:1679432] Star size: {21.5, 20}
2015-01-31 16:54:36.710 RatingViewDeveloper[6076:1679432] Star scale: 2.000000

The files in my images.xcassets are as follows:

  • ImageSet called DeselectedStar with images:
    • DeselectedStar.png
    • DeselectedStar@2x.png
    • DeselectedStar@3x.png

and the same for an ImageSet called SelectedStar.

The size it returns is always the one for the standard size image, never the 2x or even 3x version, and I do not understand why, the scale is 2.0 after all. I have tried cleaning and building again, but the result is always the crappy 1x image.

解决方案

The height and width of the image with always be of the 1x, 2x will automatically adjust itself, the resolution will double but height and width wont change.

Rename and set some other image as DeselectedStar@2x.png and you will see what I am talking about.

这篇关于UIImage imageNamed:始终以1x返回图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆