我有一个对象,它是UIImageView的子类,用于检查另一个对象上的特定图像。如果有图像,ImageView会显示图像,否则会在下载新图像时显示临时图像。下载完成后,ImageView将显示新图像。这个对象我称之为“Imager”。
我在tableview中的单元格上实例化一个Imager。 出于某种原因,成像器溢出了我的应用程序的内存。在Imager实现之前,应用程序的内存使用率大约为60Mb,但在实现后它会达到300Mb,因此我会收到内存警告,并且iPhone会关闭应用程序,就像预期一样。
我想知道,我做错了什么?这不是一个复杂的代码或逻辑,但我肯定在做一些提高内存的东西。我猜是桌面视图,但我不确定。
以下是Imager的代码: Imager.h
#import <UIKit/UIKit.h>
#import "friend.h"
#import "Group.h"
#import "Mimo.h"
@interface Imager : UIImageView
-(void)loadFriendImage:(friend*)theFriend;
-(void)loadGroupImage:(Group*)group;
@end
Imager.m
#import "Imager.h"
#import "Group.h"
@implementation Imager
-(void)loadFriendImage:(friend*)theFriend{
if(!theFriend.flagDownloaded){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
if(theFriend.userImg == nil)
[self setImage:[UIImage imageNamed:@"default_image@2x.png"]];
else{
[self setImage:theFriend.userImg];
}
});
NSLog(@"[IMAGER]: Will load %@ image.",theFriend.username);
UIImage *friendImage = [theFriend downloadImageBlocked];
if(friendImage != nil){
dispatch_sync(dispatch_get_main_queue(), ^{
[self setImage:friendImage];
});
}
});
}
else
{
NSLog(@"[IMAGER]: %@ image already exists",theFriend.username);
if(self.image == nil)
[self setImage:theFriend.userImg];
}
}
-(void)loadGroupImage:(Group*)group
{
if(!group.flagDownloaded){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
[self setImage:[UIImage imageNamed:@"default_image@2x.png"]];
});
NSLog(@"[IMAGER]: Will load %@ image.",group.groupName);
UIImage *groupImage = [group downloadImageBlocked];
if(groupImage != nil){
dispatch_sync(dispatch_get_main_queue(), ^{
[self setImage:groupImage];
});
}
});
}
else
{
[self setImage:group.groupImage];
}
}
这是使用Imager对象的表
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"friendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
Imager *accessory;
friend *workingFriend = [thisUser.friendsList objectAtIndex:indexPath.row];
cell.textLabel.text = workingFriend.username;
//----------------Setup Accessory View-----------------------------------
accessory = [[Imager alloc]init];
[accessory loadFriendImage:[thisUser.friendsList objectAtIndex:indexPath.row]];
accessory.frame = CGRectMake(cell.accessoryView.frame.origin.x, cell.accessoryView.frame.origin.y, 72.0, 72.0);
accessory.contentMode = UIViewContentModeScaleAspectFill;
accessory.clipsToBounds = YES;
cell.accessoryView = accessory;
cell.textLabel.textColor = [UIColor blackColor];
[cell.textLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:17]];
//----------------------------------------------------------
return cell;
}
这是下载图像的下载图像功能
-(UIImage*)downloadImageBlocked
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://tatohouston.azurewebsites.net/getUserImg.php"]];
[request setHTTPMethod:@"POST"];
[request addValue:@"postValues" forHTTPHeaderField:@"METHOD"];
NSDictionary *tempDic;
//---------------------------------
tempDic = @{@"userid":userID};
//serialize the dictionary data as json
NSData *dataPost = [[tempDic copy] JSONValue];
[request setHTTPBody:dataPost]; //set the data as the post body
[request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)dataPost.length] forHTTPHeaderField:@"Content-Length"];
NSData *tmpData = [[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] base64EncodedDataWithOptions:NSUTF8StringEncoding];
if(tmpData != nil){
returnData = [[NSData alloc]initWithBase64EncodedData:tmpData options:NSUTF8StringEncoding];
}
UIImage *friendImage;
if(returnData != nil){
friendImage = [UIImage imageWithData:returnData];
userImg = friendImage;
flagDownloaded = YES;
}
return friendImage;
}
答案 0 :(得分:0)
从请求返回的NSData有多大?我发现它使用的是Base64编码数据,比一般来说未编码的数据大1.37倍。我的猜测是你的图像数据非常大。