【问题标题】:Which third party Imageview class should i used?我应该使用哪个第三方 Imageview 类?
【发布时间】:2015-07-24 11:36:04
【问题描述】:

我的要求是:

  1. 需要在图像视图中显示来自服务器 URL 的图像并将其保存以供离线使用。
  2. 但可能会更新相同的 URL 图片。

我尝试了 EGOImageview、AsyncImageview、FXImageview、Haneke 等所有我尝试过的类。 我的第一部分要求已经实现。但不是第二个..即如果“xxxxx”链接图像是第一次显示....如果在同一个链接图像上更新,应用程序只显示旧图像...

【问题讨论】:

  • 你试过了吗 - [[NSURLCache sharedURLCache] removeAllCachedResponses];
  • SDWebImage 很好,从来没有遇到过问题:github.com/rs/SDWebImage
  • @Jasper 你确定 SDWebImage 会满足我的第二个要求。
  • SDWebImage的缓存系统有一些设置可以设置
  • 您如何,或者更确切地说,应用程序应该如何知道图像已更改?

标签: objective-c uiimageview imageview


【解决方案1】:

您可以使用以下课程来满足您的要求

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

这门课会做什么? 它将从服务器下载图像并将其保存到应用程序文档目录中,如果可用,则从本地获取。

如何使用它?nib 文件中为UIImageView 设置DImageView 类。

然后您可以在.m 文件中简单地使用它。

 [imgViewName processImageDataWithURLString:imageURl];

【讨论】:

    【解决方案2】:

    你应该看看SDWebImage。它是目前最常用的 UIImageView 类别之一,并提供了很多功能,包括缓存。

    查看文档的这一部分以了解如何使用它刷新缓存:Handle image refresh

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 1970-01-01
      • 2011-04-06
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 2010-09-28
      • 2017-10-09
      相关资源
      最近更新 更多