objective-c - ios开发遇到的内存过大的问题

 lou123456_541 发布于 2022-10-27 12:09
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测试了没有内存泄漏。

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

    2022-11-12 01:45 回答
  • leak一定会检测出来,你要不断地测试你的app

    2022-11-12 01:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有