objective-c - ios开发遇到的内存过大的问题
PHP中文网
PHP中文网 2017-04-18 09:43:44
0
2
419

import "memoryViewController.h"

import

import "memory.h"

@interface memoryViewController (){

NSMutableArray *theMemory; NSMutableArray *theHeadPicture; UIImage * headPicture;

}
@property (weak, nonatomic) IBOutlet UITableView *memoryTableView;

@end

@implementation memoryViewController

pragma mark -- table datasource --

//设置table行数。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if([theMemory count]==0) return 4; else { NSLog(@"输出%lu行",(unsigned long)[theMemory count]); return [theMemory count]; }

}
//设置table section数.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}
//cell
-(UITableViewCell)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

@autoreleasepool { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"memoryCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"memoryCell"]; } cell.selectionStyle = UITableViewCellSelectionStyleNone;//设置点击cell后的风格 //configure the cell if([theMemory count]==0){//加载前 //主题 UILabel *headlineLabel = (UILabel *)[cell viewWithTag:1]; headlineLabel.text = @"正在加载"; headlineLabel.textColor = [UIColor blackColor]; //时间 UILabel *dayLabel = (UILabel *)[cell viewWithTag:2]; dayLabel.text =@"正在加载"; dayLabel.textColor = [UIColor blackColor]; } else{//加载后 memory *aMemory = [[memory alloc]init]; aMemory =[theMemory objectAtIndex:indexPath.row]; //主题标签 UILabel *headlineLabel = (UILabel *)[cell viewWithTag:1]; headlineLabel.text = aMemory.headline; headlineLabel.font = [UIFont fontWithName:@"Helvetica" size:30]; headlineLabel.textColor = [UIColor whiteColor]; //时间标签 UILabel *dayLabel = (UILabel *)[cell viewWithTag:2]; dayLabel.text = [NSString stringWithFormat:@"%@",aMemory.createdAt]; dayLabel.textColor = [UIColor whiteColor]; //背景 UIView *testView = [[UIView alloc]init]; _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)]; NSLog(@"%@",_imgView); NSURL *theUrl = [NSURL URLWithString:aMemory.picture1]; _imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:theUrl]]; [testView addSubview:_imgView]; cell.backgroundView = testView; //在cell里面设置view。向View里面添加UIImageView } return cell; }

}

pragma mark --table delegate --

-(void)tableView:(UITableView)tableView didSelectRowAtIndexPath:(NSIndexPath)indexPath{

[self performSegueWithIdentifier:@"showMemoryDetail" sender:self];

}

pragma mark --getdata --

//获取全部原始数据添加到theMemory里。通过theMemory数组提取出各个cell所需要的信息。
-(void)getData{

@autoreleasepool { BmobQuery *bquery = [BmobQuery queryWithClassName:@"memory"]; //查找GameScore表的数据 [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) { for (BmobObject *obj in array) { //打印playerName memory *aMemory = [[memory alloc]init]; aMemory.createdAt = [obj objectForKey:@"createdAt"]; aMemory.headline = [obj objectForKey:@"headline"]; BmobFile *photo = [obj objectForKey:@"picture1"]; NSString *thephoto = photo.url;

// NSURL *url = [NSURL URLWithString:thephoto];
// UIImage * aHeadPicture = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
// aMemory.picture1 =aHeadPicture;
// [theMemory addObject:aMemory];

aMemory.picture1 = thephoto; [theMemory addObject:aMemory]; } [self.memoryTableView reloadData]; }]; }

}

  • (void)viewDidLoad {

    [super viewDidLoad]; theMemory = [NSMutableArray array]; theHeadPicture = [NSMutableArray array]; [self getData]; // Do any additional setup after loading the view.

    }

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.

    }

异步加载tableViewCell后内存过高,不知道问题出在哪里。我一步一步的检测代码都内存都没有增加,但是一运行起来内存就突然增高。初学者在自己尝试着,有没有知道哪里出问题的呢?我用instruments测试了没有内存泄漏。

PHP中文网
PHP中文网

认证0级讲师

全部回复 (2)
左手右手慢动作

leak一定会检测出来,你要不断地测试你的app

    黄舟

    Image的设置有问题吧 为什么拿到URL之后要转换成NSData然后在转换成Image呢,如果是网络图片的话这种操作的确存在内存峰值的问题

      最新下载
      更多>
      网站特效
      网站源码
      网站素材
      前端模板
      关于我们 免责声明 Sitemap
      PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!