Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which third party Imageview class should i used?

My requirement is :

  1. Need to show image in imageview from server URL & save it for offline use also.
  2. But There may be chances that same URL image can be updated.

I tried EGOImageview,AsyncImageview,FXImageview,Haneke thsese all classes i tried. my first part of requirement is achieved. but not second..i.e if "xxxxx" link image is shown for first time....if on that same link image is upadated ,app shows old image only...

like image 503
Kalyani Avatar asked Jul 24 '15 11:07

Kalyani


2 Answers

You can use the below class that will full fill your requirement

DImageView.h

#import <UIKit/UIKit.h>
@interface DImageView : UIImageView
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
- (void)processImageDataWithURLString:(NSString *)urlString;
+ (UIImage *)getSavedImage :(NSString *)fileName;
+ (void)saveImageWithFolderName:(NSString *)folderName AndFileName:(NSString *)fileName AndImage:(NSData *) imageData;

DImageView.m

#import "DImageView.h"
#define IMAGES_FOLDER_NAME  @"DImages"
@implementation DImageView

#pragma mark - App Life Cycle
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
    }
    return self;
}
- (void)dealloc
{
    self.activityView = nil;
    [super dealloc];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initWithFrame:[self frame]];
    }
    return self;
}

#pragma mark - Download images
- (void)processImageDataWithURLString:(NSString *)urlString //andBlock:(void (^)(UIImage * img))processImage
{
    @autoreleasepool
    {
        UIImage * saveImg = [DImageView getSavedImage:urlString];
        if (saveImg)
        {
            dispatch_queue_t callerQueue = dispatch_get_main_queue();
            dispatch_async(callerQueue, ^{

                @autoreleasepool
                {
                    [self setImage:saveImg];
                }
            });
        }
        else
        {
            [self showActivityIndicator];
            NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

            dispatch_queue_t callerQueue = dispatch_get_main_queue();
            dispatch_queue_t downloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
            __block NSError* error = nil;
            dispatch_async(downloadQueue, ^{


                NSData * imageData = [[[NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error] retain] autorelease];
                if (error)
                {
                    NSLog(@"DImg Error %@", [error debugDescription]);
                }
                else
                {
                    dispatch_async(callerQueue, ^{

                        @autoreleasepool
                        {
                            UIImage *image = [UIImage imageWithData:imageData];
                            [self setImage:image];
                            [self hideActivityIndicator];
                            /* Below code is to save image*/
                            [DImageView saveImageWithFolderName:IMAGES_FOLDER_NAME AndFileName:urlString AndImage:imageData];
                        }
                    });
                }
            });
            dispatch_release(downloadQueue);
        }
    }
}

#pragma mark - File Save methods

+ (void)saveImageWithFolderName:(NSString *)folderName AndFileName:(NSString *)fileName AndImage:(NSData *) imageData
{
    @autoreleasepool
    {
        NSFileManager *fileManger = [NSFileManager defaultManager]  ;
        NSString *directoryPath =  [NSString stringWithFormat:@"%@/%@",[DImageView  applicationDocumentsDirectory],folderName]  ;

        if (![fileManger fileExistsAtPath:directoryPath])
        {
            NSError *error = nil;
            [fileManger createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:&error];
        }

        fileName = [DImageView fileNameValidate:fileName];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@",directoryPath,fileName] ;

        BOOL isSaved = [imageData writeToFile:filePath atomically:YES];
        if (!isSaved)
        {
           NSLog(@" ** Img Not Saved");
        }
    }
}

+ (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

+ (UIImage *)getSavedImage :(NSString *)fileName
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    fileName = [DImageView fileNameValidate:fileName];

    NSFileManager * fileManger = [NSFileManager defaultManager] ;
    NSString * directoryPath =  [NSString stringWithFormat:@"%@/%@",[DImageView applicationDocumentsDirectory],IMAGES_FOLDER_NAME] ;
    NSString * filePath = [NSString stringWithFormat:@"%@/%@",directoryPath,fileName] ;

    if ([fileManger fileExistsAtPath:directoryPath])
    {
        UIImage *image = [UIImage imageWithContentsOfFile:filePath] ;
        if (image)
        {
            return image;
        }
        else
        {
            NSLog(@"** Img Not Found **");
            return nil;
        }
    }
    [pool release];
    return nil;
}

+ (NSString*) fileNameValidate : (NSString*) name
{
    name = [name stringByReplacingOccurrencesOfString:@"://" withString:@"##"];
    name = [name stringByReplacingOccurrencesOfString:@"/" withString:@"#"];
    name = [name stringByReplacingOccurrencesOfString:@"%20" withString:@""];
    return name;
}

#pragma mark - Activity Methods

- (void) showActivityIndicator
{
    self.activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    self.activityView.hidesWhenStopped = TRUE;
    self.activityView.backgroundColor = [UIColor clearColor];
    self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

    [self addSubview:self.activityView];
    [self.activityView startAnimating];
}
- (void) hideActivityIndicator
{
    CAAnimation *animation = [NSClassFromString(@"CATransition") animation];
    [animation setValue:@"kCATransitionFade" forKey:@"type"];
    animation.duration = 0.4;;
    [self.layer addAnimation:animation forKey:nil];
    [self.activityView stopAnimating];
    [self.activityView removeFromSuperview];

    for (UIView * view in self.subviews)
    {
        if([view isKindOfClass:[UIActivityIndicatorView class]])
            [view removeFromSuperview];
    }
}

What will this class do ? It will download images from server and save it to application document directory And get it from local if its available.

How to use that ? Set DImageView class in your nib file for UIImageView.

Then you can simply use it as below in your .m file.

 [imgViewName processImageDataWithURLString:imageURl];
like image 116
Hitarth Avatar answered Oct 20 '22 09:10

Hitarth


You should take a look at SDWebImage. It's one of the most used UIImageView category right now and offers a lot of features, including caching.

Have a look at this part of the documentation to learn how to refresh your cache with it: Handle image refresh

like image 32
Quentin Hayot Avatar answered Oct 20 '22 08:10

Quentin Hayot