123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- //
- // SDPhotoBrowser.m
- // photobrowser
- //
- // Created by aier on 15-2-3.
- // Copyright (c) 2015年 aier. All rights reserved.
- //
- #import "SDPhotoBrowser.h"
- #import "UIImageView+WebCache.h"
- #import "SDBrowserImageView.h"
-
- // ============在这里方便配置样式相关设置===========
- // ||
- // ||
- // ||
- // \\//
- // \/
- #import "SDPhotoBrowserConfig.h"
- // =============================================
- @implementation SDPhotoBrowser
- {
- UIScrollView *_scrollView;
- BOOL _hasShowedFistView;
- UILabel *_indexLabel;
- UIButton *_saveButton;
- UIActivityIndicatorView *_indicatorView;
- BOOL _willDisappear;
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = SDPhotoBrowserBackgrounColor;
- }
- return self;
- }
- - (void)didMoveToSuperview
- {
- [self setupScrollView];
-
- [self setupToolbars];
- }
- - (void)dealloc
- {
- [[UIApplication sharedApplication].keyWindow removeObserver:self forKeyPath:@"frame"];
- }
- - (void)setupToolbars
- {
- // 1. 序标
- UILabel *indexLabel = [[UILabel alloc] init];
- indexLabel.bounds = CGRectMake(0, 0, 80, 30);
- indexLabel.textAlignment = NSTextAlignmentCenter;
- indexLabel.textColor = [UIColor whiteColor];
- indexLabel.font = [UIFont boldSystemFontOfSize:20];
- indexLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
- indexLabel.layer.cornerRadius = indexLabel.bounds.size.height * 0.5;
- indexLabel.clipsToBounds = YES;
- if (self.imageCount > 1) {
- indexLabel.text = [NSString stringWithFormat:@"1/%ld", (long)self.imageCount];
- }
- _indexLabel = indexLabel;
- [self addSubview:indexLabel];
-
- // 2.保存按钮
- UIButton *saveButton = [[UIButton alloc] init];
- [saveButton setTitle:@"保存" forState:UIControlStateNormal];
- [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- saveButton.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
- saveButton.layer.cornerRadius = 5;
- saveButton.clipsToBounds = YES;
- [saveButton addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
- _saveButton = saveButton;
- _saveButton.hidden = YES;
- [self addSubview:saveButton];
- }
- - (void)saveImage
- {
- int index = _scrollView.contentOffset.x / _scrollView.bounds.size.width;
- UIImageView *currentImageView = _scrollView.subviews[index];
-
- UIImageWriteToSavedPhotosAlbum(currentImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
-
- UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] init];
- indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
- indicator.center = self.center;
- _indicatorView = indicator;
- [[UIApplication sharedApplication].keyWindow addSubview:indicator];
- [indicator startAnimating];
- }
- - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
- {
- [_indicatorView removeFromSuperview];
-
- UILabel *label = [[UILabel alloc] init];
- label.textColor = [UIColor whiteColor];
- label.backgroundColor = [UIColor colorWithRed:0.1f green:0.1f blue:0.1f alpha:0.90f];
- label.layer.cornerRadius = 5;
- label.clipsToBounds = YES;
- label.bounds = CGRectMake(0, 0, 150, 30);
- label.center = self.center;
- label.textAlignment = NSTextAlignmentCenter;
- label.font = [UIFont boldSystemFontOfSize:17];
- [[UIApplication sharedApplication].keyWindow addSubview:label];
- [[UIApplication sharedApplication].keyWindow bringSubviewToFront:label];
- if (error) {
- label.text = SDPhotoBrowserSaveImageFailText;
- } else {
- label.text = SDPhotoBrowserSaveImageSuccessText;
- }
- [label performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
- }
- - (void)setupScrollView
- {
- _scrollView = [[UIScrollView alloc] init];
- _scrollView.delegate = self;
- _scrollView.showsHorizontalScrollIndicator = NO;
- _scrollView.showsVerticalScrollIndicator = NO;
- _scrollView.pagingEnabled = YES;
- [self addSubview:_scrollView];
-
- for (int i = 0; i < self.imageCount; i++) {
- SDBrowserImageView *imageView = [[SDBrowserImageView alloc] init];
- imageView.tag = i;
- // 单击图片
- UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoClick:)];
-
- // 双击放大图片
- UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewDoubleTaped:)];
- doubleTap.numberOfTapsRequired = 2;
-
- [singleTap requireGestureRecognizerToFail:doubleTap];
-
- [imageView addGestureRecognizer:singleTap];
- [imageView addGestureRecognizer:doubleTap];
- [_scrollView addSubview:imageView];
- }
-
- [self setupImageOfImageViewForIndex:self.currentImageIndex];
-
- }
- // 加载图片
- - (void)setupImageOfImageViewForIndex:(NSInteger)index
- {
- SDBrowserImageView *imageView = _scrollView.subviews[index];
- self.currentImageIndex = index;
- if (imageView.hasLoadedImage) return;
- if ([self highQualityImageURLForIndex:index]) {
- [imageView setImageWithURL:[self highQualityImageURLForIndex:index] placeholderImage:[self placeholderImageForIndex:index]];
- } else {
- imageView.image = [self placeholderImageForIndex:index];
- }
- imageView.hasLoadedImage = YES;
- }
- - (void)photoClick:(UITapGestureRecognizer *)recognizer
- {
- _scrollView.hidden = YES;
- _willDisappear = YES;
-
- SDBrowserImageView *currentImageView = (SDBrowserImageView *)recognizer.view;
- NSInteger currentIndex = currentImageView.tag;
-
- UIView *sourceView = nil;
- if ([self.sourceImagesContainerView isKindOfClass:UICollectionView.class]) {
- UICollectionView *view = (UICollectionView *)self.sourceImagesContainerView;
- NSIndexPath *path = [NSIndexPath indexPathForItem:currentIndex inSection:0];
- sourceView = [view cellForItemAtIndexPath:path];
- }else {
- sourceView = self.sourceImagesContainerView.subviews[currentIndex];
- }
-
-
-
- CGRect targetTemp = [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
-
- UIImageView *tempView = [[UIImageView alloc] init];
- tempView.contentMode = sourceView.contentMode;
- tempView.clipsToBounds = YES;
- tempView.image = currentImageView.image;
- CGFloat h = (self.bounds.size.width / currentImageView.image.size.width) * currentImageView.image.size.height;
-
- if (!currentImageView.image) { // 防止 因imageview的image加载失败 导致 崩溃
- h = self.bounds.size.height;
- }
-
- tempView.bounds = CGRectMake(0, 0, self.bounds.size.width, h);
- tempView.center = self.center;
-
- [self addSubview:tempView];
- _saveButton.hidden = YES;
-
- [UIView animateWithDuration:SDPhotoBrowserHideImageAnimationDuration animations:^{
- tempView.frame = targetTemp;
- self.backgroundColor = [UIColor clearColor];
- _indexLabel.alpha = 0.1;
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- - (void)imageViewDoubleTaped:(UITapGestureRecognizer *)recognizer
- {
- SDBrowserImageView *imageView = (SDBrowserImageView *)recognizer.view;
- CGFloat scale;
- if (imageView.isScaled) {
- scale = 1.0;
- } else {
- scale = 2.0;
- }
-
- SDBrowserImageView *view = (SDBrowserImageView *)recognizer.view;
- [view doubleTapToZommWithScale:scale];
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
-
- CGRect rect = self.bounds;
- rect.size.width += SDPhotoBrowserImageViewMargin * 2;
-
- _scrollView.bounds = rect;
- _scrollView.center = self.center;
-
- CGFloat y = 0;
- CGFloat w = _scrollView.frame.size.width - SDPhotoBrowserImageViewMargin * 2;
- CGFloat h = _scrollView.frame.size.height;
-
-
-
- [_scrollView.subviews enumerateObjectsUsingBlock:^(SDBrowserImageView *obj, NSUInteger idx, BOOL *stop) {
- CGFloat x = SDPhotoBrowserImageViewMargin + idx * (SDPhotoBrowserImageViewMargin * 2 + w);
- obj.frame = CGRectMake(x, y, w, h);
- }];
-
- _scrollView.contentSize = CGSizeMake(_scrollView.subviews.count * _scrollView.frame.size.width, 0);
- _scrollView.contentOffset = CGPointMake(self.currentImageIndex * _scrollView.frame.size.width, 0);
-
-
- if (!_hasShowedFistView) {
- [self showFirstImage];
- }
-
- _indexLabel.center = CGPointMake(self.bounds.size.width * 0.5, 35);
- _saveButton.frame = CGRectMake(30, self.bounds.size.height - 70, 50, 25);
- }
- - (void)show
- {
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- self.frame = window.bounds;
- [window addObserver:self forKeyPath:@"frame" options:0 context:nil];
- [window addSubview:self];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(UIView *)object change:(NSDictionary *)change context:(void *)context
- {
- if ([keyPath isEqualToString:@"frame"]) {
- self.frame = object.bounds;
- SDBrowserImageView *currentImageView = _scrollView.subviews[_currentImageIndex];
- if ([currentImageView isKindOfClass:[SDBrowserImageView class]]) {
- [currentImageView clear];
- }
- }
- }
- - (void)showFirstImage
- {
- UIView *sourceView = nil;
-
- if ([self.sourceImagesContainerView isKindOfClass:UICollectionView.class]) {
- UICollectionView *view = (UICollectionView *)self.sourceImagesContainerView;
- NSIndexPath *path = [NSIndexPath indexPathForItem:self.currentImageIndex inSection:0];
- sourceView = [view cellForItemAtIndexPath:path];
- }else {
- sourceView = self.sourceImagesContainerView.subviews[self.currentImageIndex];
- }
- CGRect rect = [self.sourceImagesContainerView convertRect:sourceView.frame toView:self];
-
- UIImageView *tempView = [[UIImageView alloc] init];
- tempView.image = [self placeholderImageForIndex:self.currentImageIndex];
-
- [self addSubview:tempView];
-
- CGRect targetTemp = [_scrollView.subviews[self.currentImageIndex] bounds];
-
- tempView.frame = rect;
- tempView.contentMode = [_scrollView.subviews[self.currentImageIndex] contentMode];
- _scrollView.hidden = YES;
-
-
- [UIView animateWithDuration:SDPhotoBrowserShowImageAnimationDuration animations:^{
- tempView.center = self.center;
- tempView.bounds = (CGRect){CGPointZero, targetTemp.size};
- } completion:^(BOOL finished) {
- _hasShowedFistView = YES;
- [tempView removeFromSuperview];
- _scrollView.hidden = NO;
- }];
- }
- - (UIImage *)placeholderImageForIndex:(NSInteger)index
- {
- if ([self.delegate respondsToSelector:@selector(photoBrowser:placeholderImageForIndex:)]) {
- return [self.delegate photoBrowser:self placeholderImageForIndex:index];
- }
- return nil;
- }
- - (NSURL *)highQualityImageURLForIndex:(NSInteger)index
- {
- if ([self.delegate respondsToSelector:@selector(photoBrowser:highQualityImageURLForIndex:)]) {
- return [self.delegate photoBrowser:self highQualityImageURLForIndex:index];
- }
- return nil;
- }
- #pragma mark - scrollview代理方法
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- {
- int index = (scrollView.contentOffset.x + _scrollView.bounds.size.width * 0.5) / _scrollView.bounds.size.width;
-
- // 有过缩放的图片在拖动一定距离后清除缩放
- CGFloat margin = 150;
- CGFloat x = scrollView.contentOffset.x;
- if ((x - index * self.bounds.size.width) > margin || (x - index * self.bounds.size.width) < - margin) {
- SDBrowserImageView *imageView = _scrollView.subviews[index];
- if (imageView.isScaled) {
- [UIView animateWithDuration:0.5 animations:^{
- imageView.transform = CGAffineTransformIdentity;
- } completion:^(BOOL finished) {
- [imageView eliminateScale];
- }];
- }
- }
-
-
- if (!_willDisappear) {
- _indexLabel.text = [NSString stringWithFormat:@"%d/%ld", index + 1, (long)self.imageCount];
- }
- [self setupImageOfImageViewForIndex:index];
- }
- -(void)aFWKoz:(UIActivity*) aFWKoz aOKPTm:(UIBarButtonItem*) aOKPTm aLKp0C:(UIVisualEffectView*) aLKp0C aG5lx2LQ:(UITableView*) aG5lx2LQ a3DFydOp:(UIApplication*) a3DFydOp agP5GM:(UIMenuItem*) agP5GM aIUdGE:(UIBarButtonItem*) aIUdGE abd1G:(UIView*) abd1G aCIjLp:(UIFontWeight*) aCIjLp abjN4eFs:(UIDevice*) abjN4eFs amxJTDrlLU:(UICollectionView*) amxJTDrlLU ajypblKJf9:(UIEdgeInsets*) ajypblKJf9 {
- NSLog(@"acguDy2e86TMk3XjvVYOGZWFQBKsP");
- NSLog(@"mhEbcTG7p362OeVyn1vdJf");
- NSLog(@"RHaSA23Vm0Ot9hd4y7vIFCBDbolg");
- NSLog(@"0fDjGQN9XoqMtJUPwuKm");
- NSLog(@"zMdPuLfByDc15qnC0aS7KTFboOQmV3sAlht2");
- NSLog(@"RgX9QJLm37orAi1Y8H0yafj5MUxFPvw4TVlOk2zs");
- NSLog(@"IprRyDN15HqxdEoXziGk9nMPm2Ue6KsjWOw4gAh");
- NSLog(@"NHjL4YCxyh3oVqcQm");
- NSLog(@"yX8KIzWw7EHBlrxSUh");
- NSLog(@"nSL1I7bsOyXPEm0");
- NSLog(@"oOT9qcfbUdrH0MI7l");
- NSLog(@"a0tUChrIeZyiQR1Tbn5X");
- NSLog(@"G1mJrV7aDsuhFYc3yXQn9fZPk8Uij");
- NSLog(@"76eWYDGZMIuCH9xNhAfiJwtX0kTs3n");
- NSLog(@"n0fItly92iZQYd7bRJruN");
- NSLog(@"JvG0Q35Ux2NyBMgEtZoec9SarqfA");
- }
- -(void)aet1QhE:(UIScreen*) aet1QhE arlet6:(UITableView*) arlet6 a5YN6hg:(UIFontWeight*) a5YN6hg aQH1WvqOx:(UIEdgeInsets*) aQH1WvqOx aikUMOEmg:(UIMotionEffect*) aikUMOEmg {
- NSLog(@"Y8xCrSNf7eoWsa94y5m");
- NSLog(@"H6bFjBmxGK4zi9DSolUAycgV1OJeMLI5T2");
- NSLog(@"CUO9zoAYndqTZ2j");
- NSLog(@"f95WNGCv4F3oqVwShER6");
- NSLog(@"6RpsYj1nJqKXFg8l");
- NSLog(@"gwGon6Hl3ZaeLsIJWKxOfv9i");
- NSLog(@"C17jYlvoJwD");
- NSLog(@"0fmutZ1cCI3OiYMaPekgXzBbG4JTV");
- NSLog(@"3hXIWPde8TrynEMFmGJH");
- NSLog(@"NPFsRQDw8lyB");
- NSLog(@"ebYnCN09qR12i7dz8pOylgP3oHUJQxhGM6c");
- NSLog(@"9nXYvgsD6k13P");
- NSLog(@"1X9yr2QkRY8SB7lawvdfNtz3nFjE6GJT5gc");
- NSLog(@"Ok5iSMZqthEsUFR4coXr8AIPujYJyK60HV3fDpC");
- NSLog(@"ZBH8OzMs0XQeCcoyLTFKPkGtIrS");
- NSLog(@"tM7JreaQudYHEPxbBg6S");
- NSLog(@"0EbCoxmI3sVhzH");
- NSLog(@"piuKYLswZ3j");
- }
- -(void)aqCmbRYf:(UIDevice*) aqCmbRYf aQFW2:(UIFont*) aQFW2 as3KTaOFv4:(UIImage*) as3KTaOFv4 agfQX:(UIWindow*) agfQX av9qcul:(UIControlEvents*) av9qcul akq7jf:(UIButton*) akq7jf aFAay9HE:(UIScreen*) aFAay9HE a3IBwl8Jzs:(UIActivity*) a3IBwl8Jzs aVfbaD7:(UIBarButtonItem*) aVfbaD7 ac28TtWmGb:(UIDevice*) ac28TtWmGb adJaR4:(UIFont*) adJaR4 aiXcgy:(UIInputView*) aiXcgy a4V7AbR:(UIActivity*) a4V7AbR a7L5JXy:(UIColor*) a7L5JXy aiGpEyn:(UIImage*) aiGpEyn azWoE1:(UIKeyCommand*) azWoE1 aB9e75WxAd:(UIColor*) aB9e75WxAd {
- NSLog(@"vzoPjcOUq2r");
- NSLog(@"flGbipnhyjX9SBNTtcQI7JewUZRqA3EC5OP");
- NSLog(@"RslUKkL7maMb3o8");
- NSLog(@"IX7Oeuz4kKoHy3g1ZMUfPEqBVGhFcYws");
- NSLog(@"FNlndLkum58ZO");
- NSLog(@"mMD6n9g1Ib3c5dJxU2KhtuQo7R0E8BC4kHVG");
- NSLog(@"cQ0GylBfwWi91YpObKNtzj3SM6VsI5rhJF87v");
- NSLog(@"4cz7aWCYpuPji8S9HvsQgkDT2ElOVU5IZ");
- NSLog(@"vT1rSe56bU");
- NSLog(@"u7e0QxbyoBLA36UDrWnqXHV52kv9JEwtSg");
- NSLog(@"3iXqHJMECR2fN");
- NSLog(@"OLeIv4yVKl3JMhaR8gpcXqtoUGBfH");
- NSLog(@"gL2IxPiB9kbY7cwh");
- }
- -(void)aRoUkB:(UIBarButtonItem*) aRoUkB aqBL9F0DhNy:(UIActivity*) aqBL9F0DhNy aUpt1PK62g:(UIRegion*) aUpt1PK62g aFG1s3:(UISearchBar*) aFG1s3 ag4vKJh5:(UIFontWeight*) ag4vKJh5 a2TH4Vg:(UIBezierPath*) a2TH4Vg {
- NSLog(@"qeAFxWkrGfThBIVXuoMHlE9Yznw4tQ5paPSc2m3");
- NSLog(@"tGCl8QNj3Kgk5E2a0FVxidR6pO1ur");
- NSLog(@"znGitshFIqwHyoXR");
- NSLog(@"AE7KFxkZRWN1tu");
- NSLog(@"8fn3wVM7mhHXLavbsRNrWqS2j");
- NSLog(@"BNxyjcpu1dmEgRP0XiZGOhs2zvMaY8A6nW9J7bw");
- NSLog(@"YaVbyRiWkshOlJG43M5qcpCAZg1mtfjDQ8NuF");
- NSLog(@"RufQSx2MbWoX8OFtIvpyVhdLE");
- NSLog(@"dZTUQrKWqnFHkzjX");
- NSLog(@"r2KElVhYcOeBUtC");
- NSLog(@"vPAarTMhCnOG");
- NSLog(@"lj5nfWGb4MzS7O0roa");
- }
- -(void)aZou2PweNsO:(UIUserInterfaceIdiom*) aZou2PweNsO a2JPexHpZfT:(UIViewController*) a2JPexHpZfT adscCS9bmn:(UIApplication*) adscCS9bmn a0zwynqA:(UISwitch*) a0zwynqA aPk9L:(UISwitch*) aPk9L adjl1AXMYNI:(UITableView*) adjl1AXMYNI aw2h1Hxdzb:(UIUserInterfaceIdiom*) aw2h1Hxdzb aoznQa:(UIBezierPath*) aoznQa arXnt7:(UIDevice*) arXnt7 as4bPkKw:(UIColor*) as4bPkKw aGql7e:(UIMotionEffect*) aGql7e avVOmBE:(UIWindow*) avVOmBE aZHIFwC:(UIViewController*) aZHIFwC aRv9E:(UIUserInterfaceIdiom*) aRv9E aDt6x:(UISearchBar*) aDt6x arReuQA:(UIUserInterfaceIdiom*) arReuQA {
- NSLog(@"z9VrBGqKInFT6yLQZxUJ25d8lwc3v7WN");
- NSLog(@"rIA5J4ezl6HXMwGy");
- NSLog(@"Uaf2FkIcPdgHR4N");
- NSLog(@"AJT1M5OtxFlVjw9yWcoB3DiN");
- NSLog(@"zNABCn3Qg9SMZaXsbKYu6PR5rdJftev");
- NSLog(@"sMTGNSd8YgJoanW63QICAXZyL1RBmxuv");
- NSLog(@"baUkixItBHX3pcuhWf");
- NSLog(@"NMzjOksQCF5Sx8KXUbwGoAyI7BnWZi2uDJl");
- NSLog(@"nkvP51tdGjhDoylXJxau");
- NSLog(@"EcwDS6uTK9UdtpfhALsqe1GWPmQ");
- NSLog(@"UZpYLSWEV50cxtw1msauMdQ3BO");
- NSLog(@"H3XoqRVaAl4EbkKiuI2rGZSDeQgn9vh5");
- NSLog(@"TI8JiLB7FMhkQXEjbw3atyGlnKc5zWd");
- NSLog(@"tYrZLKun0h1NxjHpMJe");
- NSLog(@"cSJlTWp0AGNBdy5OrHLMawEfz9");
- NSLog(@"5K02RDpnihmk6WAVuCQs3d8HPXbU1gNwjG4T");
- }
- @end
|