Iphone 取消分配使用UIGraphicsBeginImageContext创建的图像
我正在为UIImageView创建图像数组,以便可以使用Iphone 取消分配使用UIGraphicsBeginImageContext创建的图像,iphone,ios,objective-c,animation,dealloc,Iphone,Ios,Objective C,Animation,Dealloc,我正在为UIImageView创建图像数组,以便可以使用[[u myImageView startAnimating]方法。我发现如果我像这样缓存(预加载)它,动画是流畅的 但是在我看到的工具中,游戏被释放了,但是仍然有太多的内存被分配了——我认为这是UIGraphicsBeginContext的一些片段 我用对了吗?有没有其他方法来解除锁定? 提前谢谢 - (NSMutableArray *)generateCachedImageArrayWithFilename:(NSString *)fi
[[u myImageView startAnimating]代码>方法。我发现如果我像这样缓存(预加载)它,动画是流畅的
但是在我看到的工具中,游戏被释放了,但是仍然有太多的内存被分配了——我认为这是UIGraphicsBeginContext的一些片段
我用对了吗?有没有其他方法来解除锁定?
提前谢谢
- (NSMutableArray *)generateCachedImageArrayWithFilename:(NSString *)filename extension:(NSString *)extension andImageCount:(int)count
{
_imagesArray = [[NSMutableArray alloc] init];
_fileExtension = extension;
_animationImageName = filename;
_imageCount = count;
for (int i = 0; i < count; i++)
{
NSString *tempImageNames = [NSString stringWithFormat:@"%@%i", filename, i];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:tempImageNames ofType:extension];
UIImage *frameImage = [self loadRetinaImageIfAvailable:imagePath];
UIGraphicsBeginImageContext(frameImage.size);
CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
[frameImage drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_imagesArray addObject:renderedImage];
if (_isDoublingFrames)
{
[_imagesArray addObject:renderedImage];
}
else if (_isTriplingFrames)
{
[_imagesArray addObject:renderedImage];
[_imagesArray addObject:renderedImage];
}
NSLog(@"filename = %@", filename);
}
return _imagesArray;
}
- (UIImage *)loadRetinaImageIfAvailable:(NSString *)path
{
NSString *retinaPath = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@2x.%@", [[path lastPathComponent] stringByDeletingPathExtension], [path pathExtension]]];
if ([UIScreen mainScreen].scale == 2.0 && [[NSFileManager defaultManager] fileExistsAtPath:retinaPath] == YES)
return [[UIImage alloc] initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:retinaPath]] CGImage] scale:2.0 orientation:UIImageOrientationUp];
else
return [UIImage imageWithContentsOfFile:path];
}
-(void) dealloc
{
[_imagesArray removeAllObjects];
_imagesArray = nil;
}
-(NSMutableArray*)generateCachedImageArrayWithFilename:(NSString*)文件扩展名:(NSString*)扩展名和图像计数:(int)计数
{
_imagesArray=[[NSMutableArray alloc]init];
_fileExtension=扩展;
_animationImageName=文件名;
_imageCount=计数;
for(int i=0;i
我遇到了与您编写的类似的内存问题,以及我将在您的代码中尝试的内容
1) 使用@autoreleasepool
将循环的方法或内容包装在@autoreleasepool{}
中,以创建一个本地池,该池将在方法完成后释放。在你的情况下,我觉得绕圈子更好:
for (int i = 0; i < count; i++)
{
@autoreleasepool {
// the loop code
...
}}
您可以在LoadRetinaImageAvailable
下的代码中使用imageNamed
保持这两种方法,让我们再次检查
image = [UIImage imageWithContentsOfFile:fullpath];