介紹:MJPhotoBrowser這個第三方庫是MJ老師封裝的一套用來瀏覽圖片的瀏覽器,可是是本地圖片、網絡圖片、gif圖片等,其也依賴了SDWebImage、SVProgressHUD、YLGIFImage這些三方庫,因為高度封裝,所以集成起來比較簡單,貌似已經停止更新並卸下了。下面看一些幾個重要的類:
MJPhotoBrowser框架:http://files.cnblogs.com/files/XYQ-208910/MJPhotoBrowser.zip
圖片模型類MJPhoto
MJPhoto.h
#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <QuartzCore/QuartzCore.h> #import <YLGIFImage/YLImageView.h> #import <YLGIFImage/YLGIFImage.h> #import <SDWebImage/UIImageView+WebCache.h> #import <SVProgressHUD/SVProgressHUD.h> @interface MJPhoto : NSObject @property (nonatomic, strong) NSURL *url; //圖片鏈接 @property (nonatomic, strong) UIImage *image; // 完整的圖片 @property (nonatomic, strong) UIImageView *srcImageView; // 來源view @property (nonatomic, strong, readonly) UIImage *placeholder; //占位圖片 @property (nonatomic, strong, readonly) UIImage *capture; //截圖 // 是否已經保存到相冊 @property (nonatomic, assign) BOOL save; @property (nonatomic, assign) int index; // 索引 @end
MJPhoto.m
#import "MJPhoto.h" @implementation MJPhoto #pragma mark - 截圖 - (UIImage *)capture:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } - (void)setSrcImageView:(UIImageView *)srcImageView { _srcImageView = srcImageView; _placeholder = srcImageView.image; if (srcImageView.clipsToBounds) { _capture = [self capture:srcImageView]; } } @end
預覽器類MJPhotoBrowser
MJPhotoBrowser.h
#import "MJPhoto.h" @protocol MJPhotoBrowserDelegate; @interface MJPhotoBrowser : NSObject <UIScrollViewDelegate> // 所有的圖片對象 @property (nonatomic, strong) NSArray *photos; // 當前展示的圖片索引 @property (nonatomic, assign) NSUInteger currentPhotoIndex; // 保存按鈕 @property (nonatomic, assign) NSUInteger showSaveBtn; // 顯示圖片 - (void)show; @end
集成過來使用的方法:
1.本地圖片
-(void)addImage:(UIImage *)image { [self.photos addObject:image]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; imageView.contentMode = UIViewContentModeScaleToFill; imageView.userInteractionEnabled = YES; [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]]; [self addSubview:imageView]; } //點擊預覽圖片 - (void)photoTap:(UITapGestureRecognizer *)recognizer { //1.創建圖片瀏覽器 NSMutableArray *kjphotos = [NSMutableArray array]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; //2.告訴圖片瀏覽器顯示所有的圖片 for (int i = 0 ; i < self.photos.count; i++) { //傳遞數據給瀏覽器 MJPhoto *photo = [[MJPhoto alloc] init]; photo.image = self.photos[i]; photo.srcImageView = self.subviews[i]; //設置來源哪一個UIImageView [kjphotos addObject:photo]; } brower.photos = kjphotos; //3.設置默認顯示的圖片索引 brower.currentPhotoIndex = recognizer.view.tag; //4.顯示瀏覽器 [brower show]; }
2.網絡圖片
//監聽圖片的點擊 - (void)tapPhoto:(UITapGestureRecognizer *)recognizer { //1.創建圖片瀏覽器 MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; //2.告訴圖片瀏覽器顯示所有的圖片 NSMutableArray *photos = [NSMutableArray array]; for (int i = 0 ; i < self.photos.count; i++) { Photo *pic = self.photos[i]; //傳遞數據給瀏覽器 MJPhoto *photo = [[MJPhoto alloc] init]; photo.url = [NSURL URLWithString:pic.bmiddle_pic]; photo.srcImageView = self.subviews[i]; //設置來源哪一個UIImageView [photos addObject:photo]; } brower.photos = photos; //3.設置默認顯示的圖片索引 brower.currentPhotoIndex = recognizer.view.tag; //4.顯示瀏覽器 [brower show]; }