• iOS:第三方框架MJPhotoBrowser图片浏览器的使用


    介绍: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];  
    }  
  • 相关阅读:
    如何实现多个异步同步执行?
    Treap学习笔记
    实验
    bzoj2876 [NOI2012]骑行川藏(拉格朗日乘数法)
    [SCOI2007]最大土地面积(旋转卡壳)
    Splay学习笔记
    计算几何学习笔记
    [CQOI2006]凸多边形(半平面交)
    大数的乘法(C++)
    商人过河问题(DFS)
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/5283103.html
Copyright © 2020-2023  润新知