微前端架构如何改变企业的开发模式与效率提升
819
2022-11-05
iOS开源照片浏览器框架 优化内存 支持网络图片 block数据源
SGPhotoBrowser
SGPhotoBrowser is a simple iOS photo browser with optional grid view, you can simplely inherit the SGPhotoBrowser controller to custom the display and provide the dataSource using block.
Screenshots
How to Get Started
Download SGPhotoBrowser and try out the include iPhone example app.
Installation
Drag the SGPhotoBrowser folder to your project. Attention: The SGPhotoBrowser depends on SDWebImage and MBProgressHUD, if there are these frameworks in your project, you can just delete them in the SGPhotoBrowser/Vendor folder
Usage
1.Import and inherit SGPhotoBrowser
#import "SGPhotoBrowser.h"@interface MyBrowserViewController : SGPhotoBrowser@end
2.Make an array to save the PhotoModel.
Every photo is describing by a SGPhotoModel, a SGPhotoBrowser has two URL properties, they are photoURL and thumbURL, if the photo is a local media, please use [NSURL fileURLWithPath:@""] to make the URL.
@interface MyBrowserViewController ()@property (nonatomic, strong) NSMutableArray
3.Set the dataSource callback using block.
The sg_ws() is a macro to define weakSelf, that pretend the block from cycle retain.When the models are changed, you can call the reloadData method to tell the browser to recall the dataSource.
sg_ws();[self setNumberOfPhotosHandlerBlock:^NSInteger{ return weakSelf.photoModels.count;}];[self setphotoAtIndexHandlerBlock:^SGPhotoModel *(NSInteger index) { return weakSelf.photoModels[index];}];[self setReloadHandlerBlock:^{ // add reload data code here}];[self setDeleteHandlerBlock:^(NSIndexSet *indexSet) { [weakSelf.photoModels removeObjectsAtIndexes:indexSet]; [weakSelf reloadData];}];
4.Set models to display photo from local and web.
There is a sample below to show how to set URL for local and web media.
NSMutableArray *photoModels = @[].mutableCopy;NSArray *photoURLs = @[@"http://img0.ph.126-/PgCjtjY9cStBeK-rugbj_g==/6631715378048606880.jpg", @"http://img2.ph.126-/MReos71sTqftWSZuXz_boQ==/6631554849350946263.jpg", @"http://img1.ph.126-/0Pz-IkvpsDr3lqsZGdIO4A==/6631566943978852327.jpg"];NSArray *thumbURLs = @[@"http://img2.ph.126-/q9kJFjtxcHzzJZA5EMaSUg==/6631671397583497919.png", @"http://img1.ph.126-/9blT0g2-VgAueTagWFARlA==/6631683492211398013.png", @"http://img1.ph.126-/smEiDh0FuAVQFz3rcQQdrw==/6631691188792792414.png"];// web imagesfor (NSUInteger i = 0; i < photoURLs.count; i++) { NSURL *photoURL = [NSURL URLWithString:photoURLs[i]]; NSURL *thumbURL = [NSURL URLWithString:thumbURLs[i]]; SGPhotoModel *model = [SGPhotoModel new]; model.photoURL = photoURL; model.thumbURL = thumbURL; [photoModels addObject:model];}// local imagesfor (NSUInteger i = 1; i <= 8; i++) { NSURL *photoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"photo%@",@(i)] ofType:@"jpg"]]; NSURL *thumbURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"photo%@t",@(i)] ofType:@"jpg"]]; SGPhotoModel *model = [SGPhotoModel new]; model.photoURL = photoURL; model.thumbURL = thumbURL; [photoModels addObject:model];}self.photoModels = photoModels;[self reloadData];
Custom Settings
You can set the numberOfPhotosPerRow property to set how many photos are there in every row, it's 4 by default.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~