图片的缩放源代码与使用

简介: 图片的缩放源代码与使用

这个是调用图像缩放代码片段,通过[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


目录
相关文章
pillow 压缩和放大图片 改变图片的像素
pillow 压缩和放大图片 改变图片的像素
|
6月前
|
Android开发
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
280 1
|
6月前
|
计算机视觉
OpenCV中读取、显示、保存图像及获取图像属性操作讲解及演示(附源码)
OpenCV中读取、显示、保存图像及获取图像属性操作讲解及演示(附源码)
333 0
|
6月前
|
计算机视觉 开发者 Python
OpenCV中图像的缩放与旋转讲解及实战演示(附Python源码)
OpenCV中图像的缩放与旋转讲解及实战演示(附Python源码)
152 0
|
6月前
|
Dart
Flutter 学习之图片的选择、裁切、保存
Flutter 学习之图片的选择、裁切、保存 在Flutter中,我们可以通过调用系统的图片选择器来选择一张图片,也可以通过使用插件来实现图片的裁切和保存。
344 0
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
146 0
|
计算机视觉
【方便的Opencv】实现图片合成视频+附带图片生成gif
【方便的Opencv】实现图片合成视频+附带图片生成gif
【方便的Opencv】实现图片合成视频+附带图片生成gif
|
Java API Maven
一行代码搞定图片缩略图处理
不知道大家现在工作中还有没有使用过Java处理图片的。强哥在大学毕业后,从事服务端WEB开发,就很少接触图片处理。有接触图片的,大多也就是图片的上传下载。所以,对Java处理图片相关的技术也都没怎么接触。
一行代码搞定图片缩略图处理
|
文字识别 Python wax
Python 绘图字体控制 + 文字在图片中的位置调整
Python 绘图字体控制 + 文字在图片中的位置调整
1184 0