I am newbie to iPhone programming and working on a demo app. In that I have made a sample chat module between two users. I have displayed an image with every chat bubble. All things are working fine but the only issue is when I send a message or new message is coming, all the images in all chat bubbles are loading again. I am using image cache still it's not working. Can anybody please help me to figure it out?
my code is:
- (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];