在聊天中加载图像,像whatsapp一样可以更快地本地保存 [英] Load Images in chat which is locally saved faster like whatsapp

查看:125
本文介绍了在聊天中加载图像,像whatsapp一样可以更快地本地保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天应用程序,它发送消息和文本.当前我遇到了一个问题,即从本地向表单元格加载图像的速度问题.

I am having a chat application.It send messages and text.Currently I am getting a problem which is speed issue of loading image from local to table cell.

首先我将图像保存在Document目录中以进行首次下载,然后检查是否已经存在.如果是,则从后台线程中的本地获取该图像,这使我的滚动速度很快,但当表的双端队列方法有效时然后imageview变成白色背景,片刻后图像在imageview中反射,这意味着从本地获取图像需要时间. . . .

First I save images in Document directory for the first time download.Then I am checking that is already existing.If yes then I fetch that image from local on background thread which makes my scroll fast but when deque methods for table is working then imageview got white background and after a moment images reflect in imageview it means image fetching from local is taking time.. . . . .

所以请帮我,它和whatsapp messanger应用程序一样.

So help me I need it same as whatsapp messanger app.

static NSString *simpleTableIdentifier = @"ZImageChatCell";

        ZImageChatCell *cell = (ZImageChatCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ZImageChatCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
            ChatMessageModel *messagemodel = [chatArray objectAtIndex:indexPath.row];

        if([messagemodel.ismymessage boolValue])
        {


            NSData *imgdata = [Base64 decode:messagemodel.previewimage];
            UIImage *effectImage = [UIImageEffects imageByApplyingLightEffectToImage:[UIImage imageWithData:imgdata]];
            [cell.rightimageview setImage:effectImage];


            BOOL imageexist = [self checkImageExistsInLocal:messagemodel.message];
            if(imageexist)
            {

                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
                dispatch_async(queue, ^{
                UIImage *tempimage = [self fetchImageFromLocal:messagemodel.message];
                UIImage *newimage = [APPDELEGATE imageWithImage:tempimage scaledToFillSize:CGSizeMake(175, 175)];
                    dispatch_async(dispatch_get_main_queue(), ^{

                        cell.rightimageview.image = newimage;

                    });
                });
            }
            else
            {

                if(messagemodel.message.length>0)
                {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

                        UIImage * imggot = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:messagemodel.message]]];
                        UIImage *newimage = [APPDELEGATE imageWithImage:imggot scaledToFillSize:CGSizeMake(175, 175)];
                        [self saveImageLocally:imggot withUrlString:messagemodel.message];

                        dispatch_async(dispatch_get_main_queue(), ^{

                            cell.rightimageview.image = newimage;
                            [cell.rightactivityloader stopAnimating];

                        });

                    });
                }

            }

        }
        else
        {

            NSData *imgdata = [Base64 decode:messagemodel.previewimage];
            UIImage *effectImage = [UIImageEffects imageByApplyingLightEffectToImage:[UIImage imageWithData:imgdata]];
            [cell.leftimageview setImage:effectImage];


            BOOL imageexist = [self checkImageExistsInLocal:messagemodel.message];
            if(imageexist)
            {
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
                dispatch_async(queue, ^{
                UIImage *tempimage = [self fetchImageFromLocal:messagemodel.message];
                UIImage *newimage = [APPDELEGATE imageWithImage:tempimage scaledToFillSize:CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH)];
                    dispatch_async(dispatch_get_main_queue(), ^{

                        cell.leftimageview.image = newimage;

                    });
                });
            }
            else
            {


                if(messagemodel.message.length>0)
                {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

                        UIImage * imggot = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:messagemodel.message]]];
                        UIImage *newimage = [APPDELEGATE imageWithImage:imggot scaledToFillSize:CGSizeMake(175, 175)];
                        [self saveImageLocally:imggot withUrlString:messagemodel.message];

                        dispatch_async(dispatch_get_main_queue(), ^{

                            cell.leftimageview.image = newimage;
                            [cell.leftactivityloader stopAnimating];

                        });

                    });

                }

            }

推荐答案

尝试制作用于从url下载图像的UIImageView类别文件,它将文件保存在设备的高速缓存中以及何时加载该图像再次,它将从缓存中加载该图像.

Try to make UIImageView Category file for download the image from url and it will save the file in the cache memory of the device and when you want to load that image again than it will load that image from cache memory.

UIImageView + Download.h

UIImageView+Download.h

-(void)downloadFromURL:(NSString *)url withPlaceholder:(UIImage *)placehold;

UIImageView + Download.m

UIImageView+Download.m

-(void)downloadFromURL:(NSString *)url withPlaceholder:(UIImage *)placehold
{
    if (placehold) {
        [self setImage:placehold];
    }
    if (url) {

        //
        if ([url rangeOfString:@"/Caches/"].location != NSNotFound) {
            NSData *imageData=[NSData dataWithContentsOfFile:url];
            UIImage* image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                [self setImage:image];
                [self setNeedsLayout];
            }
            return;
        }

        NSString *strImgName = [[[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] componentsSeparatedByString:@"/"] lastObject];

        NSString *imagePath = [NSString stringWithFormat:@"%@/%@",[self applicationCacheDirectoryString],strImgName];

        NSFileManager *fileManager = [NSFileManager defaultManager];

        NSString *aURL=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        if ([fileManager fileExistsAtPath:imagePath]==NO)
        {
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(queue, ^(void) {

                NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:aURL]];
                [imageData writeToFile:imagePath atomically:YES];

                UIImage* image = [[UIImage alloc] initWithData:imageData];
                if (image) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self setImage:image];
                        [self setNeedsLayout];

                    });
                }
            });
        }
        else{
            NSData *imageData=[NSData dataWithContentsOfFile:imagePath];
            UIImage* image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                [self setImage:image];
                [self setNeedsLayout];
            }
        }
    }
}

- (NSString *)applicationCacheDirectoryString
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return cacheDirectory;
}

在这里,您只需要像您的类一样调用此方法

Here you just need to call this method from your class like

[cell.imageView downloadFromURL:YOUR_IMAGE_URL withPlaceholder:PLACEHOLDER_IMAGE];

此代码对我有用

这篇关于在聊天中加载图像,像whatsapp一样可以更快地本地保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆