Iphone 如何从设置UIImage数组动画中释放内存
我想知道如何从动画阵列中释放内存,不知何故,在仪器中,动画图片消耗了24MB的真实内存。我应该怎样释放内存?在Mac OS中,图片文件的总大小约为3MB *编辑:*酷,我在构建设置中启用了ARC设置,不再崩溃…但内存使用仍然徘徊在80-120mb物理内存附近 这是为图像运行的行Iphone 如何从设置UIImage数组动画中释放内存,iphone,xcode,memory-management,uiimageview,uiimage,Iphone,Xcode,Memory Management,Uiimageview,Uiimage,我想知道如何从动画阵列中释放内存,不知何故,在仪器中,动画图片消耗了24MB的真实内存。我应该怎样释放内存?在Mac OS中,图片文件的总大小约为3MB *编辑:*酷,我在构建设置中启用了ARC设置,不再崩溃…但内存使用仍然徘徊在80-120mb物理内存附近 这是为图像运行的行 -(void)defaultSetup { self.imageArray = [[NSMutableArray alloc] initWithObjects:
-(void)defaultSetup
{
self.imageArray = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default/d7.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default/d9.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default/d11.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default/d27.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default/d6.jpg"]],
nil];
self.defaultID = [[NSMutableArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"0",nil];
self.defaultImageCaption = [[NSMutableArray alloc] initWithObjects:@"Ver",@"Green",@"Red",@"Cru",@"East",nil];
NSUInteger count = [self.defaultID count];
NSLog(@"Count %i",[self.defaultID count]);
NSMutableArray *randomImgName = self.imageArray;
NSMutableArray *randomID = self.defaultID;
NSMutableArray *randomName = self.defaultImageCaption;
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[randomName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomImgName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomID exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.imageArray = randomImgName;
self.defaultID=randomID;
self.defaultImageCaption=randomName;
NSLog(@"default filename %@",self.defaultImageCaption);
NSLog(@"default ID %@",self.defaultID);
self.imageViewTop.alpha = 1.0;
self.imageViewBottom.alpha = 0.0;
self.imageViewBottom = [[[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)] autorelease];
self.imageViewTop = [[[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)] autorelease];
[self.view addSubview:imageViewTop];
[self.view addSubview:imageViewBottom];
self.buttonCaption = [UIButton buttonWithType:UIButtonTypeCustom];
//default placement
self.buttonCaption.frame = CGRectMake(245, 365, 70, 30);
//[buttonCaption setTitle:@"\u00A9" forState:UIControlStateNormal];
[self.buttonCaption addTarget:self action:@selector(buttonCheck) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buttonCaption];
[self nextAnimation:buttonCaption.frame.size.width];
}
-(void)nextAnimation:(float)previousWidth {
//picture loop
imageViewTop.image = imageViewBottom.image;
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1];
[imageArray insertObject:imageViewBottom.image atIndex:0];
[imageArray removeLastObject];
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;
//Name Caption
NSString * tempCaption = [defaultImageCaption objectAtIndex:[defaultImageCaption count]-1];
self.dID = [defaultID objectAtIndex:[defaultID count]-1];
// make the buttons content appear in the top-left
[buttonCaption setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[buttonCaption setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter ];
[defaultImageCaption insertObject:tempCaption atIndex:0];
[defaultImageCaption removeLastObject];
[defaultID insertObject:dID atIndex:0];
[defaultID removeLastObject];
[UIView animateWithDuration:1 delay:2 options:0
animations:^{
[buttonCaption setTitle:tempCaption forState:UIControlStateNormal];
//NSLog(@"Name %@",tempCaption );
//NSLog(@"ID %@",dID);
}
completion:^(BOOL completed)
{
}];
-(无效)默认设置
{
self.imageArray=[[NSMutableArray alloc]initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“default/d7.jpg”],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“default/d9.jpg”]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“default/d11.jpg”]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“default/d27.jpg”]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“default/d6.jpg”]],
零];
self.defaultID=[[NSMutableArray alloc]initWithObjects:@“0”,“1”,“2”,“3”,“0”,nil];
self.defaultImageCaption=[[NSMutableArray alloc]initWithObjects:@“Ver”、@“Green”、@“Red”、@“Cru”、@“East”、nil];
NSU整数计数=[self.defaultID计数];
NSLog(@“计数%i”,[self.defaultID计数]);
NSMutableArray*随机imgname=self.imageArray;
NSMutableArray*randomID=self.defaultID;
NSMutableArray*randomName=self.defaultImageCaption;
对于(整数i=0;i
当使用alloc
和init
或initWith*
创建某个对象时(就像这里使用imageArray
所做的那样),您可以使用release
将其释放:
[self.imageArray release];
<>如果有“代码>自动代码<或代码>或代码>留存< /代码>调用,事情可能会变得更棘手一些。但是它们似乎并没有在这里发挥作用。 阅读可可记忆管理。(我假设你没有使用ARC(自动引用计数)。)我会考虑使用ARC,这会让你的生活更轻松。(这是一种编译时技术,因此向后兼容较旧的iOS运行时,但在较旧的iOS运行时上并没有弱的参考支持。) 总结一下内存管理规则:增加对象的保留计数的所有内容都应该通过在某个点上减少保留计数的内容来平衡 增加保留计数的内容:
self.propertyName=anInstance
,当使用retain
属性声明该属性时
[反对保留]
[Class alloc]
,[Class new]
,[objectInstance copy]
或以copy
开头的变体
将对象添加到标准集合类(例如,NSArray
)
self.imageArray = [[NSMutableArray alloc] initWithObjects:
@"default/d7.jpg",
@"default/d9.jpg",
@"default/d11.jpg",
@"default/d27.jpg",
@"default/d6.jpg",
nil];
NSString* imgPath = [imageArray objectAtIndex:[imageArray count] - 1];
imgPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:imgPath];
imageViewBottom.image = [UIImage imageWithContentsOfFile:imgPath];