自己定制加载图片并加上转圈圈
来源:互联网 发布:程序员显示器 知乎 编辑:程序博客网 时间:2024/04/30 23:15
//// HYBLoadImageView.h// CloudShopping//// Created by ljy-335 on 14-8-1.// Copyright (c) 2014年 uni2uni. All rights reserved.//#import <UIKit/UIKit.h>/*! * @brief 加载图片时,给图片加上转圈圈的状态,让用户知道图片未出现是因为网络不给力而造成的而不是没有图片 * @author huangyibiao */@interface HYBLoadImageView : UIImageView// default is showLoading- (id)initWithFrame:(CGRect)frame;- (id)initWithFrame:(CGRect)frame showLoading:(BOOL)showLoading;- (void)setImageWithURLString:(NSString *)urlString placeholder:(NSString *)placeholder;@end
//// HYBLoadImageView.m// CloudShopping//// Created by ljy-335 on 14-8-1.// Copyright (c) 2014年 uni2uni. All rights reserved.//#import "HYBLoadImageView.h"#import "ASIHTTPRequest.h"#import "HYBHttpRequestManager.h"#import "NSString+Common.h"#import "NSString+Encrypt.h"#import "NSFileManager+File.h"@interface HYBLoadImageView () <NSURLConnectionDataDelegate> { UIImageView *_networkStateAnimatingView; BOOL _showLoading; NSMutableData *_downloadData; NSURLConnection *_urlConnection;}@property (nonatomic, copy) NSString *url;@end@implementation HYBLoadImageView- (id)initWithFrame:(CGRect)frame { return [self initWithFrame:frame showLoading:YES];}- (id)initWithFrame:(CGRect)frame showLoading:(BOOL)showLoading { if (self = [super initWithFrame:frame]) { _showLoading = showLoading; self.contentMode = UIViewContentModeScaleAspectFit; } return self;}- (void)setImageWithURLString:(NSString *)urlString placeholder:(NSString *)placeholder { self.image = kImageWithName(placeholder); NSString *url = [NSString stringWithFormat:@"%@", urlString]; if (url.length == 0) { return; } self.url = url; if ([[NSFileManager defaultManager] isFileExists:[self imageCachePath]]) { if (![[NSFileManager defaultManager] isFile:[self imageCachePath] timeout:24 * 60 * 60]) { NSData *data = [[NSData alloc] initWithContentsOfFile:[self imageCachePath]]; self.image = [UIImage imageWithData:data]; return; } } _downloadData = [[NSMutableData alloc] init]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url]]; _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; [[HYBHttpRequestManager sharedRequestManager] addRequest:_urlConnection withKey:self.url.md5]; if (_showLoading) { if (_networkStateAnimatingView == nil) { CGRect frame = CGRectMake((self.frame.size.width - 22) / 2, (self.frame.size.height - 22) / 2, 22, 22); _networkStateAnimatingView = [HYBUIMaker imageViewWithFrame:frame]; [self addSubview:_networkStateAnimatingView]; NSMutableArray *imagesArray = [[NSMutableArray alloc] init]; for (int i = 1; i < 13; i++) { NSString *imgName = [NSString stringWithFormat:@"%d.png", i]; UIImage *image = kImageWithName(imgName); [imagesArray addObject:image]; } _networkStateAnimatingView.animationImages = imagesArray; _networkStateAnimatingView.animationRepeatCount = 0; _networkStateAnimatingView.animationDuration = 1.0 / (13.0 / 8.0); } [_networkStateAnimatingView startAnimating]; } return;}#pragma mark - NSURLConnectionDataDelegate- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [_downloadData setLength:0]; return;}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { [[HYBHttpRequestManager sharedRequestManager] removeRequestWithKey:self.url.md5]; self.image = [UIImage imageWithData:_downloadData]; self.alpha = 0; [UIView animateWithDuration:0.5 animations:^{ self.alpha = 1; }]; [_networkStateAnimatingView stopAnimating]; [_networkStateAnimatingView removeFromSuperview]; [_downloadData writeToFile:[self imageCachePath] atomically:YES]; return;}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [[HYBHttpRequestManager sharedRequestManager] removeRequestWithKey:self.url.md5]; [_networkStateAnimatingView stopAnimating]; [_networkStateAnimatingView removeFromSuperview]; return;}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_downloadData appendData:data]; return;}#pragma mark - 获取图片缓存路径//- (NSString *)imageCachePath { return [NSString stringWithFormat:@"%@/%@", [NSString imageCachePath], self.url.md5];}@end
0 0
- 自己定制加载图片并加上转圈圈
- 转圈圈定制加载中视图
- js实现等待加载“转圈圈”效果
- 转圈圈
- 使用纯CSS3设计加载转圈圈页面
- Android ProgressDialog 转圈圈
- 【简单常用】转圈圈,
- 转圈圈动画animation
- 第八章 转圈圈
- 给图片加载加上菊花
- iOS 转圈圈的动画
- 转圈圈(正在加载的控件)的2种创建方法
- 加载效果----转圈
- 加载转圈的代码
- jQuery 加载等待 转圈
- html 加载转圈功能
- 关于AFNetworking菊花转圈圈的问题
- Swift ActivityIndicator 转圈圈的那个玩意
- hdu2029 Palindromes _easy version
- 如何去利用Netflix的屋代理
- 采用httpclient提交数据到服务器(get/post)(无服务器) → 采用get方式提交数据到服务器
- linux系统主分区 扩展分区 和 逻辑…
- linux的解压打包命令
- 自己定制加载图片并加上转圈圈
- 关于Ubuntu里面软件无法安装
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- Vim使用技巧
- Linux反引号
- mac机装虚拟机VMware Fusion 5
- 算法学习轨迹(转)
- Linux下PATH命令行的修改
- C语言的类型转换