iOS - 组图放大查看
2019-04-19 本文已影响0人
Joh蜗牛
是个新页面,点击数组中某张图片,进入此页面:
.h文件:
#import <UIKit/UIKit.h>
@protocol ShowImgViewControllerDelegate <NSObject>
- (void)getDeleteAry:(NSMutableArray *)deleteAry;
@end
@interface ShowImgViewController : UIViewController
// 图片数组
@property (nonatomic,strong) NSMutableArray *dataSource;
// 从外界进入点击的图片坐标(第x张图片)
@property (nonatomic,assign) NSInteger index;
// 是否可删除
@property (nonatomic,assign) BOOL canDelete;
// 删除的图片数组
@property (nonatomic,strong) NSMutableArray *deleteAry;
// 代理
@property (nonatomic,weak) id<ShowImgViewControllerDelegate> delegate;
@end
.m
#import "ShowImgViewController.h"
#define K_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width// 屏宽
#define K_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height// 屏高
#define kGetNavAndStatusHeight (self.navigationController.navigationBar.frame.size.height+[[UIApplication sharedApplication] statusBarFrame].size.height)// 状态栏+导航栏高度
@interface ShowImgViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIScrollView * scrollView;
@end
@implementation ShowImgViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 删除的图片放入此数组
self.deleteAry = [@[] mutableCopy];
[self.view addSubview:self.scrollView];
// 标题展示当前图片位置,如:3/6
self.title = [NSString stringWithFormat:@"%ld/%lu",self.index+1,(unsigned long)self.dataSource.count];
// 展示图片
[self setUpImageView];
if (self.canDelete) {
// 可删除
UIButton *RightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
RightBtn.frame = CGRectMake(0, 0, 40, 40);
[RightBtn setImage:[UIImage imageNamed:@"删除-1"] forState:0];
RightBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[RightBtn addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:RightBtn];
self.navigationItem.rightBarButtonItem = item;
}
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (self.delegate && [self.delegate respondsToSelector:@selector(getDeleteAry:)]) {
// 上级页面获取到删除的图片坐标
[self.delegate getDeleteAry:self.deleteAry];
}
}
#pragma mark - 布局
- (UIScrollView *)scrollView
{
if (!_scrollView) {
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT)];
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.delegate = self;
//这里
_scrollView.contentSize = CGSizeMake(K_SCREEN_WIDTH* self.dataSource.count, K_SCREEN_HEIGHT - kGetNavAndStatusHeight);
_scrollView.contentOffset = CGPointMake(K_SCREEN_WIDTH * self.index, 0);
_scrollView.scrollEnabled = YES;
_scrollView.pagingEnabled = YES;
_scrollView.userInteractionEnabled = YES;
}
return _scrollView;
}
-(void)setUpImageView
{
for (UIView *views in [self.scrollView subviews]) {
[views removeFromSuperview];
}
int index = 0;
for (int i = 0;i < self.dataSource.count;i++)
{
UIImageView * imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(K_SCREEN_WIDTH * i, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT);
UIImage *chooseImg = self.dataSource[i];
imageView.image = chooseImg;
CGFloat imgH = chooseImg.size.height/chooseImg.size.width*K_SCREEN_WIDTH;
if (imgH > KscreenHeight) {
imgH = KscreenHeight;
}
imageView.bounds = CGRectMake(0, 0, K_SCREEN_WIDTH, imgH);
imageView.center = CGPointMake(K_SCREEN_WIDTH/2.0 + K_SCREEN_WIDTH * i, K_SCREEN_HEIGHT/2.0 - kGetNavAndStatusHeight/2);
imageView.tag = index;
[self.scrollView addSubview:imageView];
index ++;
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x/K_SCREEN_WIDTH;
self.index = index;
self.title = [NSString stringWithFormat:@"%ld/%lu",self.index+1,(unsigned long)self.dataSource.count];
}
#pragma mark - 删除
- (void)deleteAction{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认要删除吗" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.tag = 100;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
if (alertView.tag == 100) {
// 删除
if (self.dataSource.count > self.index) {
[self.deleteAry addObject:[NSString stringWithFormat:@"%ld",(long)self.index]];
[self.dataSource removeObjectAtIndex:self.index];
if (self.index > 0) {
self.index = self.index - 1;
}
self.scrollView.contentSize = CGSizeMake(K_SCREEN_WIDTH* self.dataSource.count, K_SCREEN_HEIGHT - kGetNavAndStatusHeight);
self.scrollView.contentOffset = CGPointMake(K_SCREEN_WIDTH * self.index, 0);
[self setUpImageView];
self.title = [NSString stringWithFormat:@"%ld/%lu",self.index+1,(unsigned long)self.dataSource.count];
}
}else if (alertView.tag == 200){
// 保存
if (self.dataSource.count > self.index) {
UIImage *img = self.dataSource[self.index];
// 改为png格式
NSData* imageData = UIImagePNGRepresentation(img);
UIImage* newImage = [UIImage imageWithData:imageData];
if (img != nil) {
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
}
}
}
#pragma mark - 长按保存
- (void)longPressAction:(UIGestureRecognizer *)gesture{
if (gesture.state == UIGestureRecognizerStateBegan){
// 获取点击的图片view
UIImageView *imageView = (UIImageView *)gesture.self.view;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"保存图片至本地吗" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.tag = 200;
[alert show];
}
}
// <保存到相册>
-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSString *msg = nil ;
if(error){
NSLog(@"保存失败");
}else{
NSLog(@"保存成功");
}
}
使用保存图片至本地的功能时,别忘了在plist文件中添加NSPhotoLibraryAddUsageDescription属性~~