我的图片代码是
-(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];
}
这会缓存太多内存,我在上一个问题中被告知将代码交换为使用
[UIImage imageWithContentsOfFile: GetImgWithoutCaching(@"Paddle 1.jpg")]
和
UIImage* GetImgWithoutCaching(NSString* imgName)
{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imgName ofType:nil];
return [UIImage imageWithContentsOfFile:imagePath];
}
编写代码的正确方法是什么?我将该代码放在我的.m中吗?
答案 0 :(得分:1)
首先你应该检查是否使用视网膜图片:
BOOL isHighResolution = NO;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
if ([UIScreen mainScreen].scale > 1) {
isHighResolution = YES;
}
}
如果您使用视网膜图片,请将@ 2x添加到图像名称,如下所示:
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"];
答案 1 :(得分:0)
由于您的GetImgWithoutCaching
函数返回了您需要的UIImage:
-(IBAction)start:(id)sender
{
animation.animationImages = [NSArray arrayWithObjects:
GetImgWithoutCaching(@"Paddle 1.jpg"),
GetImgWithoutCaching(@"Paddle 2.jpg"),
GetImgWithoutCaching(@"Paddle 3.jpg"),
GetImgWithoutCaching(@"Paddle 4.jpg"),
nil];
[animation setAnimationRepeatCount:0];
animation.animationDuration = 2.5;
[animation startAnimating];
}