实际上,我不确定这一点,但我有这种情况,我检查互联网是否可用,并相应地将图像加载到 AsyncImageView 中。当我从互联网加载任何图像时它工作正常,但是可以将本地图像加载到 AsyncImageView 吗?
3 回答
实际上我得到了解决这个问题的方法。我所做的就是这个
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];
也许以下内容会对您有所帮助。
创建一个新文件
继承自 UIImageView
复制/粘贴以下代码。
进口
导入“ASIHTTPRequest.h”
@interface AsynchronousImageView : UIImageView{ NSMutableData *data; UIActivityIndicatorView *activityIndicator; NSString *urlString; // 图像缓存字典的键 ASIHTTPRequest *requestLocal; @property(nonatomic,assign) UIActivityIndicatorView *activityIndicator;
- (void)loadImageFromURLString:(NSString *)theUrlString;
- (void)loadImageFromNSURL:(NSURL *)theUrl;
- (void)loadImageFromFile:(NSString *)filePath; -(无效)showLoader;-(无效)停止加载器;@结尾
然后在 .m 文件中粘贴以下内容。
导入“AsynchronousImageView.h”
导入“ASIDownloadCache.h”
@implementation AsynchronousImageView @synthesize activityIndicator;
- (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(activityIndicator == nil){ activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [自我 addSubview:activityIndicator]; activityIndicator.frame = CGRectMake(self.bounds.size.width / 2.0f - activityIndicator.frame.size.width /2.0f, self.bounds.size.height / 2.0f - activityIndicator.frame.size.height /2.0f, activityIndicator.frame.size.width,activityIndicator.frame.size.height); 【活动指标发布】;} [activityIndicator startAnimating]; } -(void) stopLoader{ if(activityIndicator){ [activityIndicator stopAnimating]; [activityIndicator 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; [自停止加载器]; 【数据发布】;数据=无;} @结尾
然后在你的 ViewComtroller .h 中导入 AsycnhronousImageView 类并写下这个
像这样制作图像的 IBOutlet。
IBOutlet AsycnhronousImageView *image;
更改 xib AsycnhronousImageView 中的类而不是 UIImage View
调用以下方法
- (void)loadImageFromFile:(NSString *)filePath;
我猜你的意思是 Nick Lockwood 的AsyncImageView ?这只是 UIImageView 的一个类别,所以当你已经有了图像时,为什么不直接设置图像属性呢?