search

Home  >  Q&A  >  body text

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

import "memoryViewController.h"

import <BmobSDK/Bmob.h>

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];
    }];
    
}

}

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

PHP中文网PHP中文网2771 days ago467

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 09:45:44

    Leak will definitely be detected, you have to constantly test your app

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:45:44

    Is there something wrong with the setting of Image? Why do you need to convert it to NSData after getting the URL and then convert it to Image? If it is a network image, this operation does have a memory peak problem

    reply
    0
  • Cancelreply