圖片瀏覽器Controller的主要思路是:新建一個 JZAlbumViewController,在這個controller裏主要顯示一個UIScrollView,這個UIScrollView用來顯示所有的圖片,
根據傳入的圖片數組imgArr來設置 contentSize
self.scrollView.contentSize =CGSizeMake(self.imgArr.count*screen_width,screen_height);
然後在自定義一個方法,根據指定的index來創建當前index的圖片頁 PhotoView,自定義一個PhotoView,繼承自UIView,裏面添加一個UIScrollView,根據傳來的圖片(可以輸URL,也可是UIImage)來創建UIImageView,並添加到UIScrollview上。
JZAlbumViewController.h 圖片瀏覽Controller
#import <UIKit/UIKit.h>
@interface JZAlbumViewController :UIViewController
/**
* 接收圖片數組,數組類型可以是url數組,image數組
*/
@property(nonatomic,strong)NSMutableArray *imgArr;
/**
* 顯示scrollView
*/
@property(nonatomic,strong)UIScrollView *scrollView;
/**
* 顯示下標
*/
@property(nonatomic,strong)UILabel *sliderLabel;
/**
* 接收當前圖片的序號,默認的是0
*/
@property(nonatomic,assign)NSInteger currentIndex;
@end
PhotoView.h 圖片View
#import <UIKit/UIKit.h>
//1.
@protocol PhotoViewDelegate <NSObject>
//點擊圖片時,隱藏圖片瀏覽器
-(void)TapHiddenPhotoView;
@end
@interface PhotoView : UIView
/**
* 添加的圖片
*/
@property(nonatomic,strong)UIImageView *imageView;
//2.
/**
* 代理
*/
@property(nonatomic,assign)id<PhotoViewDelegate> delegate;
-(id)initWithFrame:(CGRect)frame withPhotoUrl:(NSString *)photoUrl;
-(id)initWithFrame:(CGRect)frame withPhotoImage:(UIImage *)image;
@end
使用方法:
使用前請在你的工程內添加 SDWebImage MBProgress兩個第三方庫
使用的默認的圖片,圖片名稱:
comment_empty_img。如果要自定義,只需要去PhotoView裏替換掉就可以 了
#import "JZAlbumViewController.h"
在點擊圖片後添加:
JZAlbumViewController *jzAlbumVC = [[JZAlbumViewControlleralloc]init];
jzAlbumVC.currentIndex =2;//這個參數表示當前圖片的index,默認是0
jzAlbumVC.imgArr = imgArray;//圖片數組,可以是url,也可以是UIImage
[selfpresentModalViewController:jzAlbumVCanimated:YES];
效果圖:
代碼下載鏈接:http://download.csdn.net/detail/l863784757/8650157