图片的缩放源代码与使用

来源:互联网 发布:四万亿 知乎 编辑:程序博客网 时间:2024/06/07 23:13

这个是调用图像缩放代码片段,通过[self zoomImageButtonPressed : image];调用缩放图片函数就可以了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        static NSString *cellIdentifier = @"imageValueCell";        ImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        if (!imageCell) {            imageCell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier capacity:2 type:ImageCellTypeUneditable];        }            __weak typeof(self)weakSelf = self;            imageCell.addImageAction = ^(ImageControl *control ,NSInteger count) {                weakSelf.selectedControl = control;                weakSelf.imageCount ++;                [weakSelf addImage];            };            imageCell.showImageAction = ^(UIImage *image)            {                [self zoomImageButtonPressed : image];            };            imageCell.deleteImageAction = ^(ImageControl *control ,UIImage *image) {                //do something maybe                self.imageCount --;                NSMutableArray *imageArray = [self.images mutableCopy];                for (UIImage *subImage in imageArray) {                    if (subImage == image) {                        [self.images removeObject:subImage];                    }                }            };        cell = imageCell;    return cell;}

具体的缩放图片页面调用逻辑很简单就没有必要再抽象了。
- (void)zoomImageButtonPressed : (UIImage *)image
{
if (!image)
{
return;
}
else
{
NSString *aString = @”CExpandPicViewController”;

    CExpandPicViewController *expandPicViewController = [[CExpandPicViewController alloc] initWithNibName:aString bundle:nil];    expandPicViewController.image = image;    [self presentViewController:expandPicViewController animated:NO completion:nil];}

}

CExpandPicViewController.h文件的代码:

#import <UIKit/UIKit.h>@interface CExpandPicViewController : UIViewController<UIScrollViewDelegate>{}@property (nonatomic, strong) IBOutlet UIImageView *imageViewBackground;@property (nonatomic, strong) IBOutlet UIImageView *imageViewExpandPic;@property (nonatomic, strong) IBOutlet UIImage *image;@end

CExpandPicViewController.m文件的代码:

#import "CExpandPicViewController.h"@interface CExpandPicViewController () <UIScrollViewDelegate>@property (nonatomic, assign) float imageWith;@property (nonatomic, assign) float imageHeight;@property(retain,nonatomic)UIScrollView *scrollerView;@property(retain,nonatomic)UIImageView *imageView;@property(retain,nonatomic)UIImageView *imageViewBG;@end@implementation CExpandPicViewController- (void)viewDidLoad {    [super viewDidLoad];       [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];    // Do any additional setup after loading the view from its nib.    if(_image == nil)    {        return;    }    float iWidth = _image.size.width;    float iHeight = _image.size.height;    float rate = 2.0;    if((iHeight > WINDOW_HEIGHT) || (iWidth > WINDOW_WIDTH))    {        rate = 2.0;    }    else if(WINDOW_WIDTH*1000/iWidth >= (WINDOW_HEIGHT)*1000/iHeight)    {        rate = WINDOW_HEIGHT/iHeight;        if(rate < 2.0)        {            rate = 2.0;        }    }    else if(WINDOW_WIDTH*1000/iWidth < (WINDOW_HEIGHT)*1000/iHeight)    {        rate = WINDOW_WIDTH/iWidth;        if(rate < 2.0)        {            rate = 2.0;        }    }    _imageViewBackground.backgroundColor = [UIColor colorWithHex:0x000000 alpha:1.0];    _imageViewBG = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)];    _imageViewBG.backgroundColor = [UIColor colorWithHex:0x000000 alpha:1.0];    //_imageViewBG.image = _image;    _imageViewBG.userInteractionEnabled = YES;    _imageViewBG.hidden = NO;    _scrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)];    _scrollerView.delegate=self;    _scrollerView.minimumZoomScale=0.5f;    _scrollerView.maximumZoomScale= rate;    if((iHeight > WINDOW_HEIGHT) || (iWidth > WINDOW_WIDTH))    {        if(iWidth * 1000/WINDOW_WIDTH >= iHeight*1000/(WINDOW_HEIGHT))        {            iHeight = iHeight*WINDOW_WIDTH/iWidth;            iWidth = WINDOW_WIDTH;        }        else        {            iWidth = iWidth * (WINDOW_HEIGHT)/iHeight;            iHeight = WINDOW_HEIGHT;        }    }    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((WINDOW_WIDTH - iWidth)/2, (WINDOW_HEIGHT - iHeight)/2, iWidth, iHeight)];    _imageView.userInteractionEnabled = YES;    [_imageView setImage:_image];    [_scrollerView addSubview:_imageViewBG];    [_scrollerView addSubview:_imageView];    [self.view addSubview:_scrollerView];    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];    [_imageViewBG addGestureRecognizer:singleTap];    _imageViewBackground.hidden = NO;    _imageViewBackground.userInteractionEnabled = YES;    UITapGestureRecognizer *bGSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];    [_imageView addGestureRecognizer:bGSingleTap];    UITapGestureRecognizer *backGroundSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];    [_imageViewBackground addGestureRecognizer:backGroundSingleTap];}- (UIStatusBarStyle)preferredStatusBarStyle{    return UIStatusBarStyleLightContent;}- (BOOL)prefersStatusBarHidden{    return NO;}- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{    for (id view in [_scrollerView subviews]) {        if ([view isKindOfClass:[UIImageView class]]) {            {                if(_imageView ==  ((UIImageView *)view))                {                    return view;                }            }        }    }    return  nil;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView;{    if((_imageView.frame.size.width <= WINDOW_WIDTH) && (_imageView.frame.size.height <= WINDOW_HEIGHT))    {        CGPoint centerPoint = self.view.center;        _imageView.center = centerPoint;    }}- (void)onClickBackGround{    [self dismissViewControllerAnimated:NO completion:nil];}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}@end
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 平板打不开机怎么办 台电平板死机怎么办 电视指示灯不亮怎么办 手机不听使唤乱动怎么办 笔记本只有c盘怎么办 印章上印出的全是油怎么办 美工笔太粗怎么办 笔记本电脑速度太慢怎么办 电脑图形处理弱怎么办 戴尔笔记本电脑连不上网络怎么办 系统备份没有了怎么办 cad2014运行很慢怎么办 cad2007运行很慢怎么办 cad运2018行很慢怎么办 800*800图片太大怎么办 ps出现双箭头怎么办 衣服上面染上色怎么办 联想一体机忘记密码怎么办 三星a7手机黑屏怎么办 联想g40很卡怎么办 联想笔记本电脑卡机怎么办 笔记本电脑卡机了怎么办 笔记本电脑卡机动不了怎么办 ps变得很卡怎么办 戴尔笔记本卡了怎么办 戴尔电脑卡死了怎么办 win10显示器颜色不正常怎么办 没有密码重置盘怎么办 美术生英语不好怎么办 mbr分区安装不了怎么办 倒闭的共享单车怎么办 喷水壶喷嘴堵塞怎么办 电水壶按钮坏了怎么办 主机按钮坏了怎么办 手机系统删掉了怎么办 学习差的孩子怎么办 小猫生病不吃饭怎么办 深圳公寓被坑怎么办 跳芭蕾硬了怎么办 跳拉丁起反应怎么办 会认字不会写字怎么办