0

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

4

3 回答 3

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];
于 2012-08-14T13:04:30.593 回答
0

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

  1. 创建一个新文件

  2. 继承自 UIImageView

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

    进口

    导入“ASIHTTPRequest.h”

    @interface AsynchronousImageView : UIImageView{ NSMutableData *data; 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; -(无效)showLoader;-(无效)停止加载器;@结尾
  4. 然后在 .m 文件中粘贴以下内容。

    导入“AsynchronousImageView.h”

    导入“ASIDownloadCache.h”

    @implementation AsynchronousImageView @synthesize activityIndi​​cator;

    • (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // 初始化代码 //[self setContentMode:UIViewContentModeScaleAspectFit]; } 返回自我;} -(void)dealloc { if (requestLocal) { [requestLocal setDelegate:nil]; } requestLocal = nil; 活动指标 = 无;[超级释放]; } /* // 仅覆盖 drawRect: 如果您执行自定义绘图。// 空实现会对动画期间的性能产生不利影响。
    • (void)drawRect:(CGRect)rect { // 绘图代码 } */ -(void) showLoader{ if(activityIndi​​cator == nil){ activityIndi​​cator = [[UIActivityIndi​​catorView alloc]initWithActivityIndi​​catorStyle:UIActivityIndi​​catorViewStyleWhiteLarge]; [自我 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 startAnimating]; } -(void) stopLoader{ if(activityIndi​​cator){ [activityIndi​​cator stopAnimating]; [activityIndi​​cator removeFromSuperview]; 活动指标 = 无;} }
    • (void)loadImageFromFile:(NSString *)filePath { UIImage *img = [UIImage imageWithContentsOfFile:filePath]; [自我设置图像:img];[自停止加载器]; }
    • (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 *)request { requestLocal = nil; [自停止加载器]; UIImage *tempArt = [UIImage imageWithData:[request responseData]]; if (tempArt) { self.image = tempArt; } [数据发布]; 数据=无;} -(void)requestFailed:(ASIHTTPRequest *)request{ requestLocal = nil; [自停止加载器]; 【数据发布】;数据=无;} @结尾
  5. 然后在你的 ViewComtroller .h 中导入 AsycnhronousImageView 类并写下这个

    像这样制作图像的 IBOutlet。

    IBOutlet AsycnhronousImageView *image;

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

  7. 调用以下方法

    • (void)loadImageFromFile:(NSString *)filePath;
于 2012-08-14T13:25:28.257 回答
0

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

于 2012-08-14T12:47:01.580 回答