我可以将设备文档目录中的图像加载到AsyncImageView吗?

时间:2012-08-14 12:20:01

标签: iphone objective-c ios ipad xcode4

实际上,我不确定这一点,但我有这种情况,我检查互联网是否可用,并相应地将图像加载到AsyncImageView。当我从互联网上加载任何图像时它工作正常,但是可以将本地图像加载到AsyncImageView吗?

3 个答案:

答案 0 :(得分:0)

我猜你的意思是Nick Lockwood的AsyncImageView?这只是UIImageView的一个类别,所以当你已经拥有图像时,为什么不直接设置图像属性呢?

答案 1 :(得分:0)

实际上我得到了这个问题的解决方案。我所做的就是这个

 NSURL *theURL = [NSURL fileURLWithPath:@"/Users/gaurav/Library/Application Support/iPhone Simulator/5.0/Applications/AC6E9B2E-DB25-4933-A338-755B134E1A61/Documents/Attachment290_294_1344928691.jpeg"];
    NSLog(@"local URl is::%@",theURL);
    [self.asyncimageview loadImageFromURL:theURL];

答案 2 :(得分:0)

也许以下内容会对您有所帮助。

  1. 制作新文件

  2. 从UIImageView继承

  3. 复制/粘贴以下代码。

    import

    导入“ASIHTTPRequest.h”

    @interface AsynchronousImageView:UIImageView {     NSMutableData *数据;     UIActivityIndi​​catorView * activityIndi​​cator;     NSString * urlString; //用于图像缓存字典的键     ASIHTTPRequest * requestLocal; } @property(nonatomic,assign)UIActivityIndi​​catorView * activityIndi​​cator;

    • (void)loadImageFromURLString:(NSString *)theUrlString;
    • (void)loadImageFromNSURL:(NSURL *)theUrl;
    • (void)loadImageFromFile:(NSString *)filePath; - (void)showLoader; - (void)stopLoader; @end
  4. 然后在.m文件中粘贴以下内容。

    import“AsynchronousImageView.h”

    导入“ASIDownloadCache.h”

    @implementation AsynchronousImageView @synthesize activityIndi​​cator;

    • (ID)initWithFrame:方法(的CGRect)帧 { self = [super initWithFrame:frame]; if(self){     //初始化代码     // [self setContentMode:UIViewContentModeScaleAspectFit]; } 回归自我; } - (无效)的dealloc { if(requestLocal){     [requestLocal setDelegate:nil]; } requestLocal = nil; activityIndi​​cator = nil; [super dealloc]; } / * //仅覆盖drawRect:如果执行自定义绘图。 //空实现会对动画期间的性能产生负面影响。
    • (无效)的drawRect:(的CGRect)RECT { //绘图代码 } * / - (void)showLoader { if(activityIndi​​cator == nil){ activityIndi​​cator = [[UIActivityIndi​​catorView alloc] initWithActivityIndi​​catorStyle:UIActivityIndi​​catorViewStyleWhiteLarge]; [self addSubview:activityIndi​​cator]; activityIndi​​cator.frame = CGRectMake(self.bounds.size.width / 2.0f - activityIndi​​cator.frame.size.width /2.0f,self.bounds.size.height / 2.0f - activityIndi​​cator.frame.size.height /2.0f, activityIndi​​cator.frame.size.width,activityIndi​​cator.frame.size.height); [activityIndi​​cator发布]; } [activityIndi​​cator startAnimating]; } - (void)stopLoader { 如果(activityIndi​​cator){     [activityIndi​​cator stopAnimating];     [activityIndi​​cator removeFromSuperview];     activityIndi​​cator = nil; } }
    • (void)loadImageFromFile:(NSString *)filePath { UIImage * img = [UIImage imageWithContentsOfFile:filePath]; [self setImage:img]; [self stopLoader]; }
    • (void)loadImageFromURLString:(NSString *)theUrlString { / if(requestLocal){     [requestLocal setDelegate:nil]; } / requestLocal = nil;     [self showLoader];     NSURL * url = [NSURL URLWithString:theUrlString];     requestLocal = [ASIHTTPRequest requestWithURL:url];     [requestLocal setDelegate:self];     [requestLocal setDidFailSelector:@selector(requestFailed)];     [requestLocal setDownloadCache:[ASIDownloadCache sharedCache]];     [requestLocal setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];     [requestLocal startAsynchronous]; self.image = [UIImage imageNamed:@“playerDefault.png”]; }

    - (void)loadImageFromNSURL:(NSURL *)theUrl {     if(requestLocal){         [requestLocal setDelegate:nil];     }     requestLocal = nil;     requestLocal = [ASIHTTPRequest requestWithURL:theUrl];     [requestLocal setDelegate:self];     [requestLocal setDidFailSelector:@selector(requestFailed)];     [requestLocal startAsynchronous]; }

    • (void)requestFinished:(ASIHTTPRequest *)请求 { requestLocal = nil; [self stopLoader]; UIImage * tempArt = [UIImage imageWithData:[request responseData]]; if(tempArt){     self.image = tempArt; } [数据发布]; data = nil; } - (void)requestFailed:(ASIHTTPRequest *)request { requestLocal = nil; [self stopLoader]; [数据发布]; data = nil; } @end
  5. 然后在你的ViewComtroller .h导入AsycnhronousImageView类并编写这个

    像这样制作图像的IBOutlet。

    IBOutlet AsycnhronousImageView * image;

  6. 更改xib AsycnhronousImageView中的类而不是UIImage视图

  7. 调用以下方法

    • (void)loadImageFromFile:(NSString *)filePath;