iPhone中的图像缓存问题

时间:2016-01-19 06:12:30

标签: ios iphone caching image-caching

我是iPhone编程的新手并且正在使用演示应用程序。在那里,我在两个用户之间制作了一个示例聊天模块。我在每个聊天气泡中都显示了一张图片。一切正常,但唯一的问题是当我发送消息或新消息即将到来时,所有聊天气泡中的所有图像都会再次加载。我使用图像缓存仍然无法正常工作。有人可以帮我解决一下吗?

我的代码是:

- (void)setMessageFrame:(UUMessageFrame *)messageFrame
{

    if(self.btnHeadImage.image == nil)
    {
        self.btnHeadImage.image = [UIImage imageNamed:@"addUser.png"];
    }

    _messageFrame = messageFrame;

    UUMessage *message = messageFrame.message;
    self.btnHeadImage.image = [UIImage imageNamed:@"addUser.png"];

    // 1、设置时间
    self.labelTime.text = message.strTime;
    self.labelTime.frame = messageFrame.timeF;

    // 2、设置头像
    headImageBackView.frame = messageFrame.iconF;
    self.btnHeadImage.frame = CGRectMake(2, 2, ChatIconWH-4, ChatIconWH-4);

    //change by jeetu
    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:message.strIcon]];

    [self.btnHeadImage sd_setImageWithURL:nil placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        if(self.btnHeadImage.image == nil)
        {
            self.btnHeadImage.image = [UIImage imageNamed:@"addUser.png"];
        }

        dispatch_async(dispatch_get_main_queue(), ^(void)
        {
            self.btnHeadImage.image= [UIImage imageWithData:data];
              // Layout and position of just this UIImageView
        });


        //change end
        if(message.from == UUMessageFromMe)
        {
         [self.btnHeadImage setImage:[appDelegate getUserProfilePicture]];
        }
        self.btnHeadImage.clipsToBounds = YES;
        self.btnHeadImage.layer.cornerRadius = self.btnHeadImage.frame.size.width/2;

    }];


         });

    //[self.btnHeadImage setBackgroundImage:[UIImage imageNamed:@"addUser.png"] forState:UIControlStateNormal];

    // 3、设置下标
    self.labelNum.frame = messageFrame.nameF;



    if (message.from == UUMessageFromMe)
    {
        self.labelTime.backgroundColor = OUTGOING_MESSAGE_COLOR;
        //self.labelTime.backgroundColor = [UIColor colorWithRed:(205/120.0) green:(255/155.0) blue:(155/255.0) alpha:0.3];
        self.labelNum.backgroundColor = OUTGOING_MESSAGE_COLOR;
        self.btnContent.backgroundColor = OUTGOING_MESSAGE_COLOR;
    }
    else
    {
        self.labelTime.backgroundColor = INCOMING_MESSAGE_COLOR;
        self.labelNum.backgroundColor =  INCOMING_MESSAGE_COLOR;
        self.btnContent.backgroundColor = INCOMING_MESSAGE_COLOR;
    }

    //prepare for reuse
    [self.btnContent setTitle:@" " forState:UIControlStateNormal];
    self.btnContent.voiceBackView.hidden = YES;
    self.btnContent.backImageView.hidden = YES;

    self.btnContent.frame = messageFrame.contentF;


    if (message.from == UUMessageFromMe)
    {
        self.btnContent.isMyMessage = YES;
        [self.btnContent setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.btnContent.contentEdgeInsets = UIEdgeInsetsMake(ChatContentTop, ChatContentRight, ChatContentBottom, ChatContentLeft);
    }
    else
    {
        self.btnContent.isMyMessage = NO;
        [self.btnContent setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.btnContent.contentEdgeInsets = UIEdgeInsetsMake(ChatContentTop, ChatContentLeft, ChatContentBottom, ChatContentRight);
    }


    self.labelNum.text = [NSString stringWithFormat:@"     %@", message.strName];

1 个答案:

答案 0 :(得分:0)

最好,最快捷的方法是通过下载程序下载您的图像: 将图像数据仅存储在var中一次,然后存储在cellForRow委托中:

if(message.outgoing.boolvalue == YES)
{ 
  // show your profile 
  cell.imageView.image = self.myImageProfile;
}

else
{
 // you can cache it in a best place like View Did Load
 cell.imageView.image = self.recipientImage;
  }

另一种方式

创建sharedInstance类并使用方法

-(UIImage*)AvatarForUser:(NSString)
{
  // Load Data from Document or cache Folder  and recommend to cache them in Dictionary 
}

SDWebImage非常好而且快,但它是一个组件!