其中用到了两个开源代码
1.SDWebImage 异步读取图片数据
2.SCGifExample gif动画控件
此工程在SCGifExample的基础上修改
最近SDWebImage刚升级,我也借此修改了我的代码,
从中摘出gif部分与大家分享
图片缩放部分我还是用的手势,感觉以下方法不怎么好用,如果大家有更好的算法请分享
图片缩放效果请在真机上使用
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
- {
- return imageView;
- }
- - (void)scrollViewDidZoom:(UIScrollView *)scrollView
- {
- imageView.center = scrollView.center;
- if (imageView.frame.origin.x < 0)
- {
- setFrameX(imageView, 0);
- }
- if (imageView.frame.origin.y < 0)
- {
- setFrameY(imageView, 0);
- }
- }
SDWebImage内部其实也可以实现动态图片读取,我项目紧没时间改,有兴趣的可以自己改改
返回Imge时返回imageArray就可以,代码可以学习SCGifExample的实现
SDWebImage增加了4个SDWebImageManagerDelegate函数
- (void)webImageManager:(SDWebImageManager *)imageManager ActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView didFailWithError:(NSError *)error;
- (void)webImageManager:(SDWebImageManager *)imageManager ActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView didFinishWithImage:(UIImage *)image drawRoundedRect:(BOOL)bDraw;
// SDImageCache中有数据
- (void)webImageManager:(SDWebImageManager *)imageManager ActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView FileName:(NSString*)fileName didFinishWithImage:(UIImage *)image drawRoundedRect:(BOOL)bDraw;
// SDImageCache中无数据,SDWebImageDownloader下载获得
- (void)webImageManager:(SDWebImageManager *)imageManager ActivityIndicatorView:(UIActivityIndicatorView *)activityIndicatorView ImageData:(NSData *)imageData didFinishWithImage:(UIImage *)image drawRoundedRect:(BOOL)bDraw;
(UIActivityIndicatorView *)activityIndicatorView 用于显示下载状态
在ImageView调用SDWebImageManager时加载
typedef enum
{
SDWebImageRetryFailed = 1 << 0,
SDWebImageLowPriority = 1 << 1,
SDWebImageCacheMemoryOnly = 1 << 2,
SDWebImageRoundedRect = 1 << 3
} SDWebImageOptions;
中增加了SDWebImageRoundedRect用于标示是否显示圆角图片
SCGifExample增加了2个函数
- (BOOL)setGIFFile:(NSString*)gifFilePath;
- (BOOL)setGIFData:(NSData*)gifImageData;
状态栏和工具条我没有做完,状态栏最好自定义
2012-03-16:
发现了一个问题,请自行修改
UIActivityIndicatorView有可能会重复创建,会的就不需要往下看了,我只是打了个补丁
修改如下:
- - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
- {
- SDWebImageManager *manager = [SDWebImageManager sharedManager];
- // Remove in progress downloader from queue
- [manager cancelForDelegate:self];
- self.image = placeholder;
- if (url)
- {
- UIActivityIndicatorView *progressInd = (UIActivityIndicatorView *)[self viewWithTag:30326];
- if (progressInd == nil)
- {
- progressInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- progressInd.tag = 30326;
- [progressInd startAnimating];
- [self addSubview:progressInd size:CGSizeMake(37, 37)];
- [progressInd release];
- }
- //[manager downloadWithURL:url delegate:self options:options];
- [manager downloadWithURL:url ActivityIndicatorView:progressInd delegate:self options:options];
- }
- }
3月23日:
SDWebImage内部有点问题,当网址链接不是image时照样返回image图片数据
可以在
NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSData *imageData)
函数中增加对imageData的判断,是否为图片数据
非时返回nil
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
函数中再判断其值返回delegate 调用 performSelector:@selector(imageDownloader:didFailWithError:)
下载地址:点击下载
新版:点击下载
最近SDWebImage改动比较频繁,我把代码也整合了一下,简化了delegate
增加了Button部分的修改,可以设置前景背景不同状态的图片
我不做工程了,只上传SDWebImage部分
下载:SDWebImage_2.6_b78238802d.zip
好久没跟这个了发现新版SDWebImage有个大问题,会随机的阻塞下载线程导致图片不能下载成功,
老版本的那个没有问题,增加gif即可
加个gif算法:
sd_isGIF在新版SDWebImage里是有的
- - (BOOL)animatedGIFWithData:(NSData *)data
- {
- if (!data)
- {
- return NO;
- }
- if (![data sd_isGIF])
- {
- return NO;
- }
- CGImageSourceRef source = CGImageSourceCreateWithData(( CFDataRef)data, NULL);
- size_t count = CGImageSourceGetCount(source);
- NSMutableArray *images = [NSMutableArray array];
- NSTimeInterval duration = 0.0f;
- for (size_t i = 0; i < count; i++)
- {
- CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
- NSDictionary *frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
- duration += [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
- [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
- CGImageRelease(image);
- }
- CFRelease(source);
- if (!duration)
- {
- duration = (1.0f/10.0f)*count;
- }
- if (images.count > 0)
- {
- [self setImage:[images objectAtIndex:0]];
- [self setAnimationImages:images];
- [self setAnimationDuration:duration];
- [self setAnimationRepeatCount:0];
- [self startAnimating];
- return YES;
- }
- return NO;
- }