应用程序崩溃由于uiimageview

问题描述:

我有应用程序,其中我显示大约50图像,当我达到第30图像我的应用程序崩溃,由于内存问题。我尝试了所有我知道的方法,但仍然没有解决问题。所以请帮助我。应用程序崩溃由于uiimageview

NSLog(@"%d",nn); 
nn ++; 
NSLog(@"%d",nn); 
NSMutableArray* arr2 = [[NSMutableArray alloc]init]; 
arr2 = [Database executeQuery:@"select * from cartoon"]; 
if (nn == [arr2 count]) 
{ 
    nn = 0; 
} 
NSMutableDictionary* dict1 = [arr2 objectAtIndex:nn]; 
NSLog(@"%@",dict1);    
NSString * both_name = [NSString string]; 
both_name = [both_name stringByAppendingString:[dict1 objectForKey:@"mainpk"]]; 
both_name = [both_name stringByAppendingFormat:@". "]; 
both_name = [both_name stringByAppendingString:[dict1 objectForKey:@"name"]]; 
NSLog(@"both %@",both_name); 
label1.text = both_name; 
imgv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",[dict1 objectForKey:@"name"]]]; ` 
+0

这段代码与这个问题有什么关系?除显而易见外(即:如果30张图像不适合记忆,不要加载30张图像),您寻找什么样的帮助? – 2012-08-03 11:40:11

+0

检查我的答案:http://stackoverflow.com/questions/11758454/need-to-create-photo-slider-using-photo-library-photos-in-iphone/11760226#11760226可能会帮助你。 – Iducool 2012-08-03 11:57:42

+0

检查我的答案:[http://stackoverflow.com/a/11791089/1132951](http://stackoverflow.com/a/11791089/1132951)这可能会有所帮助 – 2012-08-03 12:06:30

您正在尝试将50张图像加载到内存中,而不是期望遇到麻烦? 他们的文件大小有多大?

最好是在需要/显示时加载图像。尝试从图像信息(名称,大小,其他任何地方)分割实际图像并将其加载到系统中,也许会显示一些占位符。 然后,当您显示特定图像时,将实际图像加载到内存中并显示。

代码[dict1 objectForKey:@“mainpk”]不返回字符串。

错误在你的代码:

在下面的行,你是没有密钥创建的字典。

NSMutableDictionary* dict1 = [arr2 objectAtIndex:nn]; 

然后通过无效键访问字符串。

[dict1 objectForKey:@"mainpk"]]; 

追加无效值的字符串

[both_name stringByAppendingString:[dict1 objectForKey:@"mainpk"]]; 

这会崩溃:)

这将工作:

NSMutableDictionary* dict1 = [[ NSMutableDictionary alloc ] initWithCapacity: 0]; 

[dict1 setObject: @"test" forKey: @"mainpk"]; 
[dict1 setObject: @"test1" forKey: @"name"];  

NSString * both_name = [NSString string]; 
both_name = [both_name stringByAppendingString:[dict1 objectForKey:@"mainpk"]]; 
both_name = [both_name stringByAppendingFormat:@". "]; 
both_name = [both_name stringByAppendingString:[dict1 objectForKey:@"name"]]; 
NSLog(@"both %@",both_name); 

您不能加载,很多图片一次。没有足够的内存。

  1. 只加载你是显示
  2. 不要让你的图像的任何比你需要
  3. 不要使用imageNamed(如果你需要某种形式的预览使用小缩略图)大的:,它缓存图像并吃掉内存。改用imageWithFile:。