iOS开发-ScollView实现图片缩放

来源:互联网 发布:darma公司 知乎 编辑:程序博客网 时间:2024/06/05 12:33

使用ios SDK自带的 UIScrollView 可以实现对图片的缩放

现在给大家分享我的项目中可以直接使用的组件,需要引入 afnetworking等第三方框架

关于AFNetworking大家可以自行百度,使用它的目的是下载网络图片(使用SDWebImage也可以)

<span style="font-family: SimSun; background-color: rgb(255, 255, 255);"></span><pre name="code" class="objc">使用ScorllView的代理方法可以实现缩放

<span style="font-family: SimSun; background-color: rgb(255, 255, 255);"></span>
<span style="font-family: SimSun; background-color: rgb(255, 255, 255);"></span>
,下面是一个可以直接使用的组件:

主要功能有:

显示网络图片,捏合放大或者缩小,单击关闭当前图片页面,双击放大


//  ImageDetailCon.h////  Created by  on 14/10/23.//  Copyright (c) 2014年  All rights reserved.//#import <UIKit/UIKit.h>@interface ImageDetailCon : UIViewController<UIScrollViewDelegate>    //需要使用 对应的 协议@property(strong,nonatomic)NSURL *imageURL;//给外界的接口,外界传值给ImageDetailCon  *vc;,然后present出来即可@end////  ImageDetailCon.m//  Created by  on 14/10/23.//  Copyright (c) 2014年 . All rights reserved.//#import "ImageDetailCon.h"#import <UIImageView+AFNetworking.h>//使用afnetworking框架@interface ImageDetailCon (){    UIScrollView *scrollView;    UIImageView *imageView;}@end@implementation ImageDetailCon- (void)viewDidLoad{    [super viewDidLoad];    scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];    scrollView.maximumZoomScale=5.0;//图片的放大倍数    scrollView.minimumZoomScale=1.0;//图片的最小倍率    scrollView.contentSize=CGSizeMake(self.view.bounds.size.width*1.5, self.view.bounds.size.height*1.5);    scrollView.delegate=self;    imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];    [imageView setImageWithURL:self.imageURL placeholderImage:[UIImage imageNamed:@"Fav_Img_Download"]];    [scrollView addSubview:imageView];    [self.view addSubview:scrollView];    imageView.userInteractionEnabled=YES;//注意:imageView默认是不可以交互,在这里设置为可以交互    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];    tap.numberOfTapsRequired=1;//单击    tap.numberOfTouchesRequired=1;//单点触碰    [imageView addGestureRecognizer:tap];    UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];    doubleTap.numberOfTapsRequired=2;//避免单击与双击冲突    [tap requireGestureRecognizerToFail:doubleTap];    [imageView addGestureRecognizer:doubleTap];    imageView.contentMode=UIViewContentModeScaleAspectFit;    }-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView  //委托方法,必须设置  delegate{    return imageView;//要放大的视图}​-(void)doubleTap:(id)sender{    scrollView.zoomScale=2.0;//双击放大到两倍}- (IBAction)tapImage:(id)sender{    [self dismissViewControllerAnimated:YES completion:nil];//单击图像,关闭图片详情(当前图片页面)}@end

原文地址:http://blog.csdn.net/yangbingbinga


0 0