123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- //
- // 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)aycGX:(UIDocument*) aycGX a5gRSH:(UIActivity*) a5gRSH aS1NBFzJaO:(UIControlEvents*) aS1NBFzJaO akgAOcuK:(UISearchBar*) akgAOcuK afAGh:(UIEdgeInsets*) afAGh apaTtP:(UIUserInterfaceIdiom*) apaTtP {
- NSLog(@"xIDghniROVaH31uST9W");
- NSLog(@"AlHDg5nx10QSfVIzGBTCFEo");
- NSLog(@"JELZzfWN5qIwn6mRAcTsXtC70vb8hugKU3eFol");
- NSLog(@"82dzbV3QcAIvgWNkhZt9fPayUoBeqliKujT7RO");
- NSLog(@"7vTkCmAZriJQ21KPbI6dclzxDqh0");
- NSLog(@"kwNeWZMrVz06bqB");
- NSLog(@"FmZMKhyGY1AXjI7l3tTxH9Lgw4");
- NSLog(@"VHLKmJiWg4xrSecyak20AsXUF6ZQBu1");
- NSLog(@"Ydj7Su53xszZUmo9yQNi0Mw");
- NSLog(@"IumzoOkNESF2V5Z3fnX7HtGArliDWhexd");
- NSLog(@"IBNhueixyUg6H1MbVGZjSvtYfXn9w");
- NSLog(@"MJI5Fd9Xa7kgunrpTWvm8");
- NSLog(@"pj6dwza9GSTxHou1yZcFvKRfrhPbV");
- }
- -(void)aAUbX5Wdhgt:(UIScreen*) aAUbX5Wdhgt alnTedOH:(UIVisualEffectView*) alnTedOH aqpvDRi:(UISearchBar*) aqpvDRi apt07lMX:(UIImage*) apt07lMX aEqtuy:(UIUserInterfaceIdiom*) aEqtuy a6kZ9DCufz:(UIColor*) a6kZ9DCufz aDMigcsd:(UIMotionEffect*) aDMigcsd aYRfrtA:(UICollectionView*) aYRfrtA aXscC2lm:(UIColor*) aXscC2lm a6DQHZr:(UIAlertView*) a6DQHZr ay74mw:(UICollectionView*) ay74mw arRoQ6:(UIView*) arRoQ6 acirY1k:(UIMenuItem*) acirY1k aQ4xg1as8:(UIColor*) aQ4xg1as8 {
- NSLog(@"mf89cWDh2FRIvalUL0BydJ51G3TQ");
- NSLog(@"szPolpFjMN39DeCGi7RYk1tKEqyaUQVc0dOhTuHL");
- NSLog(@"TfRKLoJ63l0QszevpEuAYPDnmUFbgV1");
- NSLog(@"IZrucGFCRUVxMtjDkg5lLSAQBXpzEeyN");
- NSLog(@"yjhRHE9KLDV7tC3IiroBx6NZ4");
- NSLog(@"mRTPlyufkgNZvaXxBQSdeOWIMLE");
- NSLog(@"tIjR98Uvg7qGbOscfQNnS5AKrDapV");
- NSLog(@"uPBQ6t1jUVfYbWFNskqi7OMXDevEwlGp");
- NSLog(@"brPVEzJQB7SZ");
- NSLog(@"4QvHLD9NueFitOr5A3ZVgqf2snw6YxPXkEl");
- NSLog(@"dLKH4hFAkqW");
- NSLog(@"beaIMVqiGsBrCDTSd9AULc4Rxg1");
- NSLog(@"uHYOElKG5Fs1no");
- NSLog(@"yjP8tEaBFJqsiN2TDZeU");
- NSLog(@"opsDH74uvXKMG");
- NSLog(@"zc2jaL1slQtC8MYuSnXpkxA40VmwKU9dyWEv");
- NSLog(@"mETX6hu3BjOV9cbIDM2l5CgnGo0dxeLvRYZ");
- NSLog(@"A34abyPsvWxE7D6Qcj2gKG");
- NSLog(@"xGmSJ8TgYv");
- }
- -(void)aC0oZhs:(UIFont*) aC0oZhs avcetEgOpx2:(UIBezierPath*) avcetEgOpx2 a7AcpIqxl:(UIWindow*) a7AcpIqxl aRPcMwAsO:(UIView*) aRPcMwAsO ahobr:(UIVisualEffectView*) ahobr aT3PzvLf5:(UIEvent*) aT3PzvLf5 aorWaX:(UIEdgeInsets*) aorWaX aEf9tM3CJi:(UIDevice*) aEf9tM3CJi ao8DwketX:(UIView*) ao8DwketX aGbaxc:(UISwitch*) aGbaxc aWeCN:(UIActivity*) aWeCN aagsy1rvF:(UISwitch*) aagsy1rvF {
- NSLog(@"4hMB06IJZLwRETFj5Vui");
- NSLog(@"ql3wiMtaovgSsk4Wh5UfADOdI6r");
- NSLog(@"OmX7szJAyUPGDwV0EvWT6be8Np2lg");
- NSLog(@"Qvt0pjfzdBxL7CR6NGwelXFaE5K");
- NSLog(@"82zJynX39fMTGHQi7N");
- NSLog(@"LjX0itQ6FbDUlJh3VmdCyP9IGpMkua");
- NSLog(@"3QJVFohCStINXPEg");
- NSLog(@"c5yXSiRnEfz8WYPAbBvrGUpOZtkx741s6FNmjVCd");
- NSLog(@"Tnwi0rgUbN8Iyx5WqLfSO");
- NSLog(@"4e7QJNvfV563Rob");
- NSLog(@"D3yc7ZqboCYdKURAGtT6pnJlw");
- NSLog(@"p8QZEvhk4uWbyzeaDm56loMLrKNjgVtT9");
- NSLog(@"60LZWzMJl3UdCscbPVfi9p45nHThSe2xIoaO8tg");
- NSLog(@"FoCMQfn1gDYGm2BtsZLR6Ty5");
- NSLog(@"E9B1VhGXg6PZS2m4aRdvY");
- NSLog(@"PaC29fZDpNAnbQ6YGkqmH0LBIS");
- }
- -(void)azb2YdoNsw:(UISwitch*) azb2YdoNsw aZpUrg:(UISearchBar*) aZpUrg az1Ocn4bpqv:(UIFontWeight*) az1Ocn4bpqv apb4EHJU7Dv:(UIViewController*) apb4EHJU7Dv aQTXeVv:(UISearchBar*) aQTXeVv aAnOcG9I:(UIScreen*) aAnOcG9I aTMoV:(UISwitch*) aTMoV akQYyH4Fxm:(UIControl*) akQYyH4Fxm aNbPlvZ0WF7:(UIMenuItem*) aNbPlvZ0WF7 a920JmVv7WE:(UICollectionView*) a920JmVv7WE a7IfOA0zQC8:(UIBarButtonItem*) a7IfOA0zQC8 aVTAhx:(UIDocument*) aVTAhx aiOQaM39:(UILabel*) aiOQaM39 ahBLTzD:(UIUserInterfaceIdiom*) ahBLTzD {
- NSLog(@"5U07iv6kjtB9yPJp1uHgD2RfQ8AMNax3enSOl");
- NSLog(@"oZcEWiagR2h");
- NSLog(@"MfADO95akgoywEhs4ZmVub");
- NSLog(@"VI62ijeFPy");
- NSLog(@"aq7yRvsGjhJugOF6");
- NSLog(@"JsrQxk0UdSe15oaLuIhDiP2HvbMcjRTNFA7XqWgG");
- NSLog(@"dgp3qRPs0XKGL6tM8V4iu");
- NSLog(@"VSr1N3YJ5s8vF2jg9aiKnCM");
- NSLog(@"J8pYUArnbsXCeZRTLmo463jEM0");
- NSLog(@"MNHX9R1Vq7");
- NSLog(@"UT3AceBxrLId");
- NSLog(@"NASI0RYuT1jvtqf7mGUhaXQyCPMDWHrbE");
- NSLog(@"YoD2fmESgj7AitlawL1dMCGVNyOkQxFK56pXcv93");
- NSLog(@"GNyz4ovBJhXd3O8IluCtLq5QFZcpiWeasHYx");
- NSLog(@"jz9xpqSMewkLYGy18AW3NgPVRmsCH2Tta4BidcI");
- NSLog(@"WgBLztQyFf836xCeO71jRuodU02JrG4sPkS");
- }
- -(void)atMimfu:(UIActivity*) atMimfu a9iCYT:(UIImageView*) a9iCYT amZL5fpX:(UILabel*) amZL5fpX arLXPH9AieM:(UIImageView*) arLXPH9AieM aUnOfiFBR:(UIControlEvents*) aUnOfiFBR adKJpkvrONE:(UIControl*) adKJpkvrONE awb0nOU:(UIRegion*) awb0nOU {
- NSLog(@"AS7dgKMoOHV");
- NSLog(@"woqlhe3zcXyFi");
- NSLog(@"erBbY4S3xaUzmOEID8ojnFuw");
- NSLog(@"SFvwIRpklQW6oNLKjg5hXt8Bfq3DMC1n4aTArJE");
- NSLog(@"ed3Aqc2C74Wp");
- NSLog(@"VwOocSGWit9jUz40kuF8hgHLNXQ");
- NSLog(@"p89lde3mWMszVGOF");
- NSLog(@"fU2x5tAIkHh8mOK01aL9cDPQZ");
- NSLog(@"SLDp6ky2b80X5C14YPitNfwqjcZex9EVHJTB");
- NSLog(@"0msnvM8f21YDO");
- NSLog(@"Lb0yVGX4YEN");
- NSLog(@"1SB6uZeGtxAL7z9sdfmj");
- NSLog(@"19FkSwd03cuX84LBnNzgaMoiemH5rEJyhI");
- NSLog(@"Qme6H0ikTKJ1LlW4w2X8q93IYBxAjtUrVSDanb");
- NSLog(@"8UzWAqtojYlODmhR0i9Ef2JXFV");
- }
- @end
|