gpt4 book ai didi

ios - 将 imageNamed 转换为 imageWithContentsOfFile :

转载 作者:行者123 更新时间:2023-11-29 03:14:35 24 4
gpt4 key购买 nike

我的图像代码是

-(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 中?

最佳答案

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

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"];

关于ios - 将 imageNamed 转换为 imageWithContentsOfFile :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21822520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com