#import <UIKit/UIKit.h>
@class DDProgressView;
@interface ZoomImageView : UIImageView<NSURLConnectionDataDelegate>
{
@private
UIScrollView *_scrollView;
UIButton *_saveButton;
UIImageView *_fullImageView;
DDProgressView *_prpgressView;
double _length;//请求图片的长度
NSMutableData *_data;//当前图片请求下来的数据
}
@property(nonatomic,copy)NSString *urlString;//大图片的地址
//添加方法手势(需要给大图片的网址)
- (void)addZoom:(NSString *)urlString;
@end
#import "ZoomImageView.h"
#import "DDProgressView.h"
#import "MBProgressHUD.h"
@implementation ZoomImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)_initViews
{
//滑动视图
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_scrollView.backgroundColor = [UIColor blackColor];
[self.window addSubview:_scrollView];
//添加缩小手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zoomOutAction)];
[_scrollView addGestureRecognizer:tap];
[tap release];
}
//大图片视图
if (_fullImageView == nil) {
_fullImageView = [[UIImageView alloc]initWithImage:self.image];
_fullImageView.contentMode = UIViewContentModeScaleAspectFit;
_fullImageView.userInteractionEnabled = YES;
[_scrollView addSubview:_fullImageView];
}
//进度条
if (_prpgressView == nil) {
_prpgressView = [[DDProgressView alloc]initWithFrame:CGRectMake(10, kScreenHeight / 2, kScreenWidth - 20, 50)];
[_prpgressView setOuterColor:[UIColor redColor]];
[_prpgressView setInnerColor:[UIColor yellowColor]];
[_prpgressView setEmptyColor:[UIColor grayColor]];
_prpgressView.progress = 0;
[_scrollView addSubview: _prpgressView];
}
//保存按钮
if (_saveButton == nil) {
_saveButton = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
[_saveButton setImage:[UIImage imageNamed:@"compose_savebutton_background.png"] forState:UIControlStateNormal];
[_saveButton setImage:[UIImage imageNamed:@"compose_savebutton_background_highlighted.png"] forState:UIControlStateHighlighted];
_saveButton.frame = CGRectMake(kScreenWidth - 23 - 20, kScreenHeight - 19 - 20, 23, 19);
[_saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:_saveButton];
}
}
//保存图片到本地相册
- (void)saveAction
{
UIImage *image = _fullImageView.image;
if (image != nil) {
//做一个保存提示
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
hud.labelText = @"正在保存...";
hud.dimBackground = YES;
//将图片保存到相册
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), hud);
}
}
//图片保存相册成功调用的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
MBProgressHUD *hud = contextInfo;
hud.customView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
hud.mode = MBProgressHUDModeCustomView;
hud.labelText = @"保存完成";
//延迟1秒钟关闭
[hud hide:YES afterDelay:1.0];
}
- (void)savePhoto:(MBProgressHUD *)hud
{
NSLog(@"保存完成");
}
//添加方法手势(需要给大图片的网址)
- (void)addZoom:(NSString *)urlString
{
self.urlString = urlString;
//添加手势
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(zoomInAction)];
[self addGestureRecognizer:tap];
[tap release];
}
//放大手势
- (void)zoomInAction
{
[self _initViews];
[_data release];
_data = [[NSMutableData alloc]init];
//设置大图片开始的frame
CGRect frame = [self convertRect:self.bounds toView:self.window];
_fullImageView.frame = frame;
_scrollView.backgroundColor = [UIColor clearColor];
_saveButton.hidden = YES;
_prpgressView.hidden = YES;
[UIView animateWithDuration:.3 animations:^{
_fullImageView.frame = [UIScreen mainScreen].bounds;
_scrollView.backgroundColor = [UIColor blackColor];
} completion:^(BOOL finished) {
_saveButton.hidden = NO;
_prpgressView.hidden = NO;
}];
//请求网络图片
NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[NSURLConnection connectionWithRequest:request delegate:self];
}
//缩小手势
- (void)zoomOutAction
{
//去掉保存按钮
[_saveButton removeFromSuperview];
[_saveButton release];
_saveButton = nil;
//如果进度条没有移除我们就移除
if (_prpgressView != nil) {
[_prpgressView removeFromSuperview];
[_prpgressView release];
_prpgressView = nil;
}
[UIView animateWithDuration:.3 animations:^{
_fullImageView.frame = [self convertRect:self.bounds toView:self.window];
_scrollView.backgroundColor = [UIColor clearColor];
} completion:^(BOOL finished) {
//移除视图
[_scrollView removeFromSuperview];
[_scrollView release];
_scrollView = nil;
[_fullImageView removeFromSuperview];
[_fullImageView release];
_fullImageView = nil;
}];
}
#pragma mark - NSURLConnectionDataDelegate
//服务器响应时调用的协议方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpRespone = (NSHTTPURLResponse *)response;
NSDictionary *dic = [httpRespone allHeaderFields];
NSLog(@"dic:%@",dic);
NSString *size = [dic objectForKey:@"Content-Length"];
_length = [size doubleValue];
}
//接受数据时调用的协议方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
_prpgressView.progress = _data.length / _length;
}
//数据加载完成之后调用的协议方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *image = [UIImage imageWithData:_data];
_fullImageView.image = image;
//加载完成之后隐藏进度条
_prpgressView.progress = 1;
_prpgressView.hidden = YES;
[_prpgressView removeFromSuperview];
[_prpgressView release];
_prpgressView = nil;
//图片等比例拉宽
//图片的高度 / 图片的宽度 =将要显示视图的高度 / 将要显示视图的宽度
float height = image.size.height / image.size.width * kScreenWidth;
_fullImageView.size = CGSizeMake(kScreenWidth, height);
_scrollView.contentSize = CGSizeMake(kScreenWidth, height);
}
@end