转换imageNamed到imageWithContentsOfFile: [英] Converting imageNamed to imageWithContentsOfFile:

查看:72
本文介绍了转换imageNamed到imageWithContentsOfFile:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code为我的图片是

My code for my images is

-(IBAction)start:(id)sender
{
    animation.animationImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"Paddle 1.png"],
                                 [UIImage imageNamed:@"Paddle 2.png"],
                                 [UIImage imageNamed:@"Paddle 3.png"],
                                 [UIImage imageNamed:@"Paddle 4.png"],
nil];
    [animation setAnimationRepeatCount:0];
    animation.animationDuration = 2.5;
    [animation startAnimating];
}

这是缓存的内存太多,我是在一个previous的问题告诉我交换code使用

This is caching too much memory and I was told in a previous question to swap my code to using

[UIImage imageWithContentsOfFile: GetImgWithoutCaching(@"Paddle 1.jpg")]

UIImage* GetImgWithoutCaching(NSString* imgName)
{
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
    return [UIImage imageWithContentsOfFile:imagePath];
}

什么是写code的正确方法?我做的地方,code在我的.m的是什么?

What is the correct way of writing the code? do i place that code in my .m as is?

推荐答案

首先,你应该检查是否采用视网膜图片:

First you should check if using retina picture:

BOOL isHighResolution = NO;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([UIScreen mainScreen].scale > 1) {
        isHighResolution = YES;
    }
}

如果您使用的是视网膜图像,添加@ 2倍图像的名字,如:

if you are using retina picture, add @2x to image name, like this:

NSString *noExtFileName = [name stringByDeletingPathExtension];
if (isHighResolution) {
    if (![noExtFileName hasSuffix:@"@2x"]) {
       noExtFileName = [noExtFileName stringByAppendingString:@"@2x"];
    }
}

 //if image only "png" type
return [[NSBundle mainBundle] pathForResource:noExtFileName ofType:@"png"];

这篇关于转换imageNamed到imageWithContentsOfFile:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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