@interface memoryViewController (){
NSMutableArray *theMemory;
NSMutableArray *theHeadPicture;
UIImage * headPicture;
}
@property (weak, nonatomic) IBOutlet UITableView *memoryTableView;
@end
@implementation memoryViewController
//设置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;
}
}
-(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
[self performSegueWithIdentifier:@"showMemoryDetail" sender:self];
}
//获取全部原始数据添加到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测试了没有内存泄漏。
黄舟2017-04-18 09:45:44
画像の設定に何か問題がありますか? URL を取得した後でそれを NSData に変換し、それから画像に変換する必要があるのはなぜですか? それがネットワーク イメージの場合、この操作にはメモリ ピークの問題があります。