Ios 将UIImage添加到动画的NSMutable数组
我有一个应用程序,可以从项目导航器中存储在组中的图像创建动画(而不是Ios 将UIImage添加到动画的NSMutable数组,ios,objective-c,animation,memory-leaks,uiimage,Ios,Objective C,Animation,Memory Leaks,Uiimage,我有一个应用程序,可以从项目导航器中存储在组中的图像创建动画(而不是images.xcsets)。此代码“工作”的原因是它可以正确设置动画,但使用imageNamed会导致内存泄漏,因为图像文件未被释放 我不明白为什么使用imageNamed:添加可以将图像添加到我的数组中,但是imageWithContentsOfFile:不能 关于我的应用程序机制的一些信息: self.myPhoto设置在另一个ViewController的序列上。图像的数量可能会有所不同,因此在将文件添加到数组之前,我会
images.xcsets
)。此代码“工作”的原因是它可以正确设置动画,但使用imageNamed
会导致内存泄漏,因为图像文件未被释放
我不明白为什么使用imageNamed:
添加可以将图像添加到我的数组中,但是imageWithContentsOfFile:
不能
关于我的应用程序机制的一些信息:
self.myPhoto
设置在另一个ViewController
的序列上。图像的数量可能会有所不同,因此在将文件添加到数组之前,我会测试文件是否“存在”
文件名遵循以下命名约定:
"1-1.jpg"
"2-1.jpg"
"2-2.jpg"
"99-1.jpg"
"99-2.jpg"
"99-3.jpg"
"99-4.jpg"
此代码正常工作,但映像未解除分配,导致内存泄漏:
- (void)startAnimation {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d.jpg", self.myPhoto, imageNumber];
// check if a file exists
if ([UIImage imageNamed:fileName]) {
// if it exists, add it to the array
[imageArray addObject:[UIImage imageNamed:fileName]];
} else {
// otherwise, don't add image to the array
break;
}
}
self.myImageView.animationImages = imageArray;
self.myImageView.animationDuration = 1.5f;
self.myImageView.animationRepeatCount = 0;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.myImageView startAnimating];
}
我在上面运行了一些仪器,发现我的动画中出现了内存泄漏。仔细研究一下StackOverflow,我发现我将文件添加到myArray
的方式导致图像无法被释放
所以我尝试了这个:
- (void)startAnimation {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
NSString *fileName = [NSString stringWithFormat:@"%@-%d", self.myPhoto, imageNumber];
// check if a file exists
if ([UIImage imageNamed:fileName]) {
// if it exists, add it to the array
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@"jpg"]]];
NSLog(@"%@ added to imageArray", fileName);
} else {
// otherwise, don't add image to the array
break;
}
}
NSLog(@"There are %lu images in imageArray", (unsigned long)imageArray.count);
self.myImageView.animationImages = imageArray;
self.myImageView.animationDuration = 1.5f;
self.myImageView.animationRepeatCount = 0;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.myImageView startAnimating];
}
当我这样做的时候,动画加载的页面会出现,但是图像不会被添加到我的数组中。这是一个有据可查的问题。以下是一些关于这个问题的帖子:
谢谢你的阅读。虽然我相信这个问题的解决方案是我一个愚蠢得惊人的疏忽,但我还是被难住了。证明我是对的;) 出于绝望,我做了一些小改动,无意中找到了“答案”。备注:我在此处进行了更改:
- (void)startAnimation {
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
// I set the filename here, adding .jpg to it
NSString *fileName = [NSString stringWithFormat:@"%@-%d.jpg", self.myPhoto, imageNumber];
// check if a file exists
if ([UIImage imageNamed:fileName]) {
// if it exists, add it to the array
// I blanked out ofType, as it's set when I create the local fileName variable
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@""]]];
} else {
// otherwise, don't add image to the array
break;
}
}
self.myImageView.animationImages = imageArray;
self.myImageView.animationDuration = 1.5f;
self.myImageView.animationRepeatCount = 0;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.myImageView startAnimating];
}
我认为可能只是您的路径在开始时需要有[[NSBundle mainBundle]resourcePath]?编辑:啊,没关系,被变量的名称误导:PSILY问题,但你使用的是ARC,对吗?您链接的两个问题都是ARC之前的问题,我想我使用的是ARC。我正在运行Xcode 6.4,部署目标8.1(当我没有想法时,尝试了8.4用于S和G),对于第二种方法,您是否获得了正确数量的照片?还是数组只是空的?如果它是空的,请尝试记录每个图像,看看它是否存在。可能是你使用了错误的资源路径我知道了!在这里发布答案。它是有效的,我不知道为什么会有效,但“正确”的方法无法完成工作。奇怪的是,仅仅在文件名末尾添加.jpg就可以使它工作了。我在创建本地
filename
变量时,尝试了使用和不使用该变量,然后在addObject
中尝试了它,并意外地发现了它。谢谢你的思考和阅读。