1

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];
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91

1 Answers1

0

The best and quick way for you is Download your Image by your downloader then : store your image data in var only once , then in your cellForRow delegate :

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

Another Way

create sharedInstance class and wrtie a method

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

the SDWebImage is very good and quick but it is a component!

Mo Farhand
  • 1,144
  • 8
  • 21
  • Thank farhand for reply,but i am beginner so can you please help me which changes i should make to my code pls.pls. – Concentrated H2SO4 Jan 19 '16 at 06:40
  • @ConcentratedH2SO4 the best way : you should download the avatar of your user with a downloader! then cache them with a unique key then load them by my way that i mention in my post! alternative way : you can use SDWebImageCache and cache them with unique key then load them :) – Mo Farhand Jan 19 '16 at 06:45