Skip to content

Commit

Permalink
适配iPhone X,优化初次启动进入相册速度
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Sep 22, 2017
1 parent 2b88fc6 commit 5b73d12
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 303 deletions.
10 changes: 4 additions & 6 deletions PhotoBrowser/ZLBigImageCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,6 @@ - (void)loadImage:(id)obj

- (void)resetSubviewSize:(id)obj
{
self.containerView.frame = CGRectMake(0, 0, kViewWidth, 0);

CGRect frame;

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
Expand All @@ -478,13 +476,13 @@ - (void)resetSubviewSize:(id)obj
if (orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight) {
orientationIsUpOrDown = NO;
CGFloat height = MIN(kViewHeight, h);
CGFloat height = MIN(GetViewHeight(self), h);
frame.origin = CGPointZero;
frame.size.height = height;
UIImage *image = self.imageView.image;

CGFloat imageScale = image.size.width/image.size.height;
CGFloat screenScale = kViewWidth/kViewHeight;
CGFloat screenScale = kViewWidth/GetViewHeight(self);

if (imageScale > screenScale) {
frame.size.width = floorf(height * imageScale);
Expand All @@ -506,7 +504,7 @@ - (void)resetSubviewSize:(id)obj
UIImage *image = self.imageView.image;

CGFloat imageScale = image.size.height/image.size.width;
CGFloat screenScale = kViewHeight/kViewWidth;
CGFloat screenScale = GetViewHeight(self)/kViewWidth;

if (imageScale > screenScale) {
frame.size.height = floorf(width * imageScale);
Expand All @@ -525,7 +523,7 @@ - (void)resetSubviewSize:(id)obj

CGSize contentSize;
if (orientationIsUpOrDown) {
contentSize = CGSizeMake(width, MAX(kViewHeight, frame.size.height));
contentSize = CGSizeMake(width, MAX(GetViewHeight(self), frame.size.height));
if (frame.size.height < GetViewHeight(self)) {
self.containerView.center = CGPointMake(GetViewWidth(self)/2, GetViewHeight(self)/2);
} else {
Expand Down
3 changes: 3 additions & 0 deletions PhotoBrowser/ZLDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#define weakify(var) __weak typeof(var) weakSelf = var
#define strongify(var) __strong typeof(var) strongSelf = var

#define ZL_IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define ZL_IS_IPHONE_X (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 812.0f)

#define kZLPhotoBrowserBundle [NSBundle bundleForClass:[self class]]

// 图片路径
Expand Down
26 changes: 17 additions & 9 deletions PhotoBrowser/ZLEditVideoController.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ - (void)viewDidLoad {
[self setupUI];
[self analysisAssetImages];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
Expand All @@ -270,16 +270,25 @@ - (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

self.playerLayer.frame = CGRectMake(15, 30, kViewWidth-30, kViewHeight-160);
UIEdgeInsets inset = UIEdgeInsetsZero;
if (@available(iOS 11, *)) {
inset = self.view.safeAreaInsets;
}

self.playerLayer.frame = CGRectMake(15, inset.top>0?inset.top:30, kViewWidth-30, kViewHeight-160-inset.bottom);

self.editView.frame = CGRectMake((kViewWidth-kItemWidth*10)/2, kViewHeight-100, kItemWidth*10, kItemHeight);
self.editView.frame = CGRectMake((kViewWidth-kItemWidth*10)/2, kViewHeight-100-inset.bottom, kItemWidth*10, kItemHeight);
self.editView.validRect = self.editView.bounds;
self.collectionView.frame = CGRectMake(0, kViewHeight-100, kViewWidth, kItemHeight);
[self.collectionView setContentInset:UIEdgeInsetsMake(0, (kViewWidth-kItemWidth*10)/2, 0, (kViewWidth-kItemWidth*10)/2)];
[self.collectionView setContentOffset:CGPointMake(_offsetX-(kViewWidth-kItemWidth*10)/2, 0)];
self.collectionView.frame = CGRectMake(inset.left, kViewHeight-100-inset.bottom, kViewWidth-inset.left-inset.right, kItemHeight);

CGFloat leftOffset = ((kViewWidth-kItemWidth*10)/2-inset.left);
CGFloat rightOffset = ((kViewWidth-kItemWidth*10)/2-inset.right);
[self.collectionView setContentInset:UIEdgeInsetsMake(0, leftOffset, 0, rightOffset)];
[self.collectionView setContentOffset:CGPointMake(_offsetX-leftOffset, 0)];

_bottomView.frame = CGRectMake(0, kViewHeight-44, kViewWidth, kItemHeight);
_doneBtn.frame = CGRectMake(kViewWidth - 70, 7, 60, 30);
_bottomView.frame = CGRectMake(0, kViewHeight-44-inset.bottom, kViewWidth, kItemHeight);
_cancelBtn.frame = CGRectMake(10+inset.left, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 15, YES, 30), 30);
_doneBtn.frame = CGRectMake(kViewWidth-70-inset.right, 7, 60, 30);
}

#pragma mark - notifies
Expand Down Expand Up @@ -345,7 +354,6 @@ - (void)creatBottomView
[self.view addSubview:_bottomView];

_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_cancelBtn.frame = CGRectMake(10, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 15, YES, 30), 30);
_cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_cancelBtn setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCancelText) forState:UIControlStateNormal];
Expand Down
22 changes: 15 additions & 7 deletions PhotoBrowser/ZLEditViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,32 @@ - (void)viewWillDisappear:(BOOL)animated
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];

UIEdgeInsets inset = UIEdgeInsetsZero;
if (@available(iOS 11, *)) {
inset = self.view.safeAreaInsets;
}

CGFloat w = kViewWidth-20;
CGFloat maxH = kViewHeight-128-inset.bottom-inset.top-50;
CGFloat h = w * self.model.asset.pixelHeight / self.model.asset.pixelWidth;
if (h > kViewHeight-180) {
h = kViewHeight-180;
if (h > maxH) {
h = maxH;
w = h * self.model.asset.pixelWidth / self.model.asset.pixelHeight;
}
_imageView.frame = CGRectMake((kViewWidth-w)/2, (kViewHeight-h)/2-60, w, h);
_gridLayer.frame = _imageView.bounds;
[self clippingRatioDidChange];
_bottomView.frame = CGRectMake(0, kViewHeight-44, kViewWidth, 44);

_bottomView.frame = CGRectMake(0, kViewHeight-44-inset.bottom, kViewWidth, 44);
_cancelBtn.frame = CGRectMake(10+inset.left, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 15, YES, 30), 30);
_saveBtn.frame = CGRectMake(kViewWidth/2-20, 7, 40, 30);
_doneBtn.frame = CGRectMake(kViewWidth - 70, 7, 60, 30);
_doneBtn.frame = CGRectMake(kViewWidth-70-inset.right, 7, 60, 30);

_indicator.center = _imageView.center;

_rotateBtn.superview.frame = CGRectMake(kViewWidth-70, kViewHeight-128, 70, 80);
_menuScroll.frame = CGRectMake(0, kViewHeight-128, kViewWidth-70, 80);
_rotateBtn.superview.frame = CGRectMake(kViewWidth-70-inset.right, kViewHeight-128-inset.bottom, 70, 80);
_menuScroll.frame = CGRectMake(inset.left, kViewHeight-128-inset.bottom, kViewWidth-70-inset.left-inset.right, 80);
}

- (void)initUI
Expand Down Expand Up @@ -340,7 +349,6 @@ - (void)creatBottomView
[self.view addSubview:_bottomView];

_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_cancelBtn.frame = CGRectMake(10, 7, GetMatchValue(GetLocalLanguageTextValue(ZLPhotoBrowserCancelText), 15, YES, 30), 30);
_cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_cancelBtn setTitle:GetLocalLanguageTextValue(ZLPhotoBrowserCancelText) forState:UIControlStateNormal];
Expand Down
5 changes: 3 additions & 2 deletions PhotoBrowser/ZLPhotoActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ NS_ASSUME_NONNULL_BEGIN
/**
提供 预览照片网络/本地图片
@param photos 接收对象 UIImage / NSURL
@param photos 接收对象 UIImage / NSURL(网络url或本地图片url)
@param index 点击的照片索引
@param hideToolBar 是否隐藏底部工具栏和导航右上角选择按钮
@param complete 回调 (数组内为接收的UIImage / NSUrl 对象)
*/
- (void)previewPhotos:(NSArray *)photos index:(NSInteger)index complete:(void (^)(NSArray *photos))complete;
- (void)previewPhotos:(NSArray *)photos index:(NSInteger)index hideToolBar:(BOOL)hideToolBar complete:(void (^)(NSArray *photos))complete;

NS_ASSUME_NONNULL_END

Expand Down
19 changes: 15 additions & 4 deletions PhotoBrowser/ZLPhotoActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ @interface ZLPhotoActionSheet () <UICollectionViewDelegateFlowLayout, UIImagePic
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verColHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *verBottomSpace;


@property (nonatomic, assign) BOOL animate;
Expand Down Expand Up @@ -265,7 +266,7 @@ - (void)previewSelectedPhotos:(NSArray<UIImage *> *)photos assets:(NSArray<PHAss
}];
}

- (void)previewPhotos:(NSArray *)photos index:(NSInteger)index complete:(nonnull void (^)(NSArray * _Nonnull))complete
- (void)previewPhotos:(NSArray *)photos index:(NSInteger)index hideToolBar:(BOOL)hideToolBar complete:(nonnull void (^)(NSArray * _Nonnull))complete
{
[self.arrSelectedModels removeAllObjects];
for (id obj in photos) {
Expand All @@ -280,6 +281,7 @@ - (void)previewPhotos:(NSArray *)photos index:(NSInteger)index complete:(nonnull
[self.arrSelectedModels addObject:model];
}
ZLShowBigImgViewController *svc = [self pushBigImageToPreview:photos index:index];
svc.hideToolBar = hideToolBar;
__weak typeof(svc.navigationController) weakNav = svc.navigationController;
[svc setPreviewNetImageBlock:^(NSArray *photos) {
__strong typeof(weakNav) strongNav = weakNav;
Expand Down Expand Up @@ -346,8 +348,13 @@ - (void)show
}

if (self.animate) {
UIEdgeInsets inset = UIEdgeInsetsZero;
if (@available(iOS 11, *)) {
inset = self.sender.view.safeAreaInsets;
[self.verBottomSpace setConstant:inset.bottom];
}
__block CGRect frame = self.baseView.frame;
frame.origin.y += kBaseViewHeight;
frame.origin.y = kViewHeight;
self.baseView.frame = frame;
[UIView animateWithDuration:0.2 animations:^{
frame.origin.y -= kBaseViewHeight;
Expand All @@ -359,8 +366,12 @@ - (void)show
- (void)hide
{
if (self.animate) {
UIEdgeInsets inset = UIEdgeInsetsZero;
if (@available(iOS 11, *)) {
inset = self.sender.view.safeAreaInsets;
}
__block CGRect frame = self.baseView.frame;
frame.origin.y += kBaseViewHeight;
frame.origin.y += (kBaseViewHeight+inset.bottom);
[UIView animateWithDuration:0.2 animations:^{
self.baseView.frame = frame;
} completion:^(BOOL finished) {
Expand Down Expand Up @@ -709,7 +720,7 @@ - (void)pushThumbnailViewController
{
ZLPhotoBrowser *photoBrowser = [[ZLPhotoBrowser alloc] initWithStyle:UITableViewStylePlain];
ZLImageNavigationController *nav = [self getImageNavWithRootVC:photoBrowser];
ZLThumbnailViewController *tvc = [[ZLThumbnailViewController alloc] initWithNibName:@"ZLThumbnailViewController" bundle:kZLPhotoBrowserBundle];
ZLThumbnailViewController *tvc = [[ZLThumbnailViewController alloc] init];
[nav pushViewController:tvc animated:YES];
[self.sender presentViewController:nav animated:YES completion:nil];
}
Expand Down
2 changes: 1 addition & 1 deletion PhotoBrowser/ZLPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ - (void)pushThumbnailVCWithIndex:(NSInteger)index animated:(BOOL)animated
{
ZLAlbumListModel *model = self.arrayDataSources[index];

ZLThumbnailViewController *tvc = [[ZLThumbnailViewController alloc] initWithNibName:@"ZLThumbnailViewController" bundle:kZLPhotoBrowserBundle];
ZLThumbnailViewController *tvc = [[ZLThumbnailViewController alloc] init];
tvc.albumListModel = model;

[self.navigationController showViewController:tvc sender:self];
Expand Down
3 changes: 2 additions & 1 deletion PhotoBrowser/ZLPhotoBrowserCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ - (void)setModel:(ZLAlbumListModel *)model
}

weakify(self);
[ZLPhotoManager requestImageForAsset:model.headImageAsset size:CGSizeMake(GetViewWidth(self)*3, GetViewHeight(self)*3) completion:^(UIImage *image, NSDictionary *info) {

[ZLPhotoManager requestImageForAsset:model.headImageAsset size:CGSizeMake(GetViewWidth(self)*2.5, GetViewHeight(self)*2.5) completion:^(UIImage *image, NSDictionary *info) {
strongify(weakSelf);
strongSelf.headImageView.image = image;
}];
Expand Down
6 changes: 6 additions & 0 deletions PhotoBrowser/ZLPhotoManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
*/
+ (ZLAlbumListModel *)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage;


/**
block 获取相机胶卷相册列表对象
*/
+ (void)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage complete:(void (^)(ZLAlbumListModel *album))complete;

/**
* @brief 获取用户所有相册列表
*/
Expand Down
7 changes: 7 additions & 0 deletions PhotoBrowser/ZLPhotoManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ + (ZLAlbumListModel *)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectI
return m;
}

+ (void)getCameraRollAlbumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage complete:(void (^)(ZLAlbumListModel *))complete
{
if (complete) {
complete([self getCameraRollAlbumList:allowSelectVideo allowSelectImage:allowSelectImage]);
}
}

+ (void)getPhotoAblumList:(BOOL)allowSelectVideo allowSelectImage:(BOOL)allowSelectImage complete:(void (^)(NSArray<ZLAlbumListModel *> *))complete
{
if (!allowSelectImage && !allowSelectVideo) {
Expand Down
3 changes: 3 additions & 0 deletions PhotoBrowser/ZLShowBigImgViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
//点击选择后的图片预览数组,预览相册图片时为 UIImage,预览网络图片时候为UIImage/NSUrl
@property (nonatomic, strong) NSMutableArray *arrSelPhotos;

/**预览 网络/本地 图片时候是否 隐藏底部工具栏和导航右上角按钮*/
@property (nonatomic, assign) BOOL hideToolBar;

//预览相册图片回调
@property (nonatomic, copy) void (^btnDonePreviewBlock)(NSArray<UIImage *> *, NSArray<PHAsset *> *);

Expand Down
Loading

0 comments on commit 5b73d12

Please sign in to comment.