图片的缩放源代码与使用
来源:互联网 发布:四万亿 知乎 编辑:程序博客网 时间: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
- 图片的缩放源代码与使用
- 缩放图片:Matrix与Bitmap的使用
- HSlider 从中心缩放图片的源代码
- Martix精确缩放图片:Matrix与Bitmap的使用
- 图片的缩放与旋转
- SDL图片的旋转与缩放
- Android图片的拖拽与缩放
- Android图片的拖拽与缩放
- iOS 图片的缩放与居中
- 图片的单指拖拽与双指缩放
- Android 图片压缩与缩放的方法
- PhotoView图片的缩放与滑动
- chapter14_2图片的旋转与缩放
- Brush画图的使用 和 图片缩放
- 使用SeekBar监听图片的缩放情况
- Android PhotoView 支持图片缩放的使用
- 187,使用手势控制图片的缩放
- 使用Matlab实现对图片的缩放
- java 注解2
- node.js中的process.nextTick()和setImmediate()
- 快速排序算法
- eclipse的使用技巧(一)
- 维基百科简体中文语料的获取
- 图片的缩放源代码与使用
- hdu 1753 大明A+B
- el 表达式判断
- 使用bitmap处理海量数据
- JS测试框架-Jasmine简介
- 异常的应用
- Zookeeper(1)
- Windows下Python安装MySQLdb
- 事件捕获与事件冒泡