Skip to content

Commit 93cc03f

Browse files
committed
1、整理 QMUIAlbumViewController 的注释及代码;2、优化 QMUINavigationController 的 log
1 parent 4d63e60 commit 93cc03f

5 files changed

Lines changed: 59 additions & 42 deletions

File tree

QMUIKit/UIComponents/ImagePickerLibrary/QMUIAlbumViewController.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern const UIEdgeInsets QMUIAlbumTableViewCellDefaultAlbumNameInsets;
2929
@protocol QMUIAlbumViewControllerDelegate <NSObject>
3030

3131
@required
32+
/// 点击相簿里某一行时,需要给一个 QMUIImagePickerViewController 对象用于展示九宫格图片列表
3233
- (QMUIImagePickerViewController *)imagePickerViewControllerForAlbumViewController:(QMUIAlbumViewController *)albumViewController;
3334

3435
@optional
@@ -49,15 +50,20 @@ extern const UIEdgeInsets QMUIAlbumTableViewCellDefaultAlbumNameInsets;
4950
@end
5051

5152
/**
52-
* 当前设备照片里的相簿列表
53+
* 当前设备照片里的相簿列表,使用方式:
54+
* 1. 使用 init 初始化。
55+
* 2. 指定一个 albumViewControllerDelegate,并实现 @required 方法。
56+
*
57+
* @warning 注意,iOS 访问相册需要得到授权,建议先询问用户授权,通过了再进行 QMUIAlbumViewController 的初始化工作。关于授权的代码,可参考 QMUI Demo 项目里的 [QDImagePickerExampleViewController authorizationPresentAlbumViewControllerWithTitle] 方法。
58+
* @see [QMUIAssetsManager requestAuthorization:]
5359
*/
5460
@interface QMUIAlbumViewController : QMUICommonTableViewController
5561

5662
@property(nonatomic, assign) CGFloat albumTableViewCellHeight UI_APPEARANCE_SELECTOR; // 相册列表 cell 的高度,同时也是相册预览图的宽高
5763

5864
@property(nonatomic, weak) id<QMUIAlbumViewControllerDelegate> albumViewControllerDelegate;
5965

60-
@property(nonatomic, assign) QMUIAlbumContentType contentType; // 相册展示内容的类型,可以控制只展示照片、视频或音频(仅 iOS 8.0 及以上版本支持)的其中一种,也可以同时展示所有类型的资源
66+
@property(nonatomic, assign) QMUIAlbumContentType contentType; // 相册展示内容的类型,可以控制只展示照片、视频或音频(仅 iOS 8.0 及以上版本支持)的其中一种,也可以同时展示所有类型的资源,默认展示所有类型的资源。
6167

6268
@property(nonatomic, copy) NSString *tipTextWhenNoPhotosAuthorization;
6369
@property(nonatomic, copy) NSString *tipTextWhenPhotosEmpty;

QMUIKit/UIComponents/ImagePickerLibrary/QMUIAlbumViewController.m

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,13 @@ @implementation QMUIAlbumViewController {
129129
BOOL _usePhotoKit;
130130
}
131131

132-
- (instancetype)initWithStyle:(UITableViewStyle)style {
133-
if (self = [super initWithStyle:style]) {
134-
_usePhotoKit = IOS_VERSION >= 8.0;
135-
if (albumViewControllerAppearance) {
136-
// 避免 albumViewControllerAppearance init 时走到这里来,导致死循环
137-
self.albumTableViewCellHeight = [QMUIAlbumViewController appearance].albumTableViewCellHeight;
138-
}
132+
- (void)didInitialized {
133+
[super didInitialized];
134+
_usePhotoKit = IOS_VERSION >= 8.0;
135+
if (albumViewControllerAppearance) {
136+
// 避免 albumViewControllerAppearance init 时走到这里来,导致死循环
137+
self.albumTableViewCellHeight = [QMUIAlbumViewController appearance].albumTableViewCellHeight;
139138
}
140-
return self;
141139
}
142140

143141
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
@@ -157,15 +155,16 @@ - (void)viewDidLoad {
157155
[super viewDidLoad];
158156
if ([QMUIAssetsManager authorizationStatus] == QMUIAssetAuthorizationStatusNotAuthorized) {
159157
// 如果没有获取访问授权,或者访问授权状态已经被明确禁止,则显示提示语,引导用户开启授权
160-
if (!self.tipTextWhenNoPhotosAuthorization) {
158+
NSString *tipString = self.tipTextWhenNoPhotosAuthorization;
159+
if (!tipString) {
161160
NSDictionary *mainInfoDictionary = [[NSBundle mainBundle] infoDictionary];
162161
NSString *appName = [mainInfoDictionary objectForKey:@"CFBundleDisplayName"];
163162
if (!appName) {
164163
appName = [mainInfoDictionary objectForKey:(NSString *)kCFBundleNameKey];
165164
}
166-
self.tipTextWhenNoPhotosAuthorization = [NSString stringWithFormat:@"请在设备的\"设置-隐私-照片\"选项中,允许%@访问你的手机相册", appName];
165+
tipString = [NSString stringWithFormat:@"请在设备的\"设置-隐私-照片\"选项中,允许%@访问你的手机相册", appName];
167166
}
168-
[self showEmptyViewWithText:self.tipTextWhenNoPhotosAuthorization detailText:nil buttonTitle:nil buttonAction:nil];
167+
[self showEmptyViewWithText:tipString detailText:nil buttonTitle:nil buttonAction:nil];
169168
} else {
170169

171170
_albumsArray = [[NSMutableArray alloc] init];
@@ -184,17 +183,11 @@ - (void)refreshAlbumAndShowEmptyTipIfNeed {
184183
if ([_albumsArray count] > 0) {
185184
[self.tableView reloadData];
186185
} else {
187-
if (!self.tipTextWhenPhotosEmpty) {
188-
self.tipTextWhenPhotosEmpty = @"空照片";
189-
}
190-
[self showEmptyViewWithText:self.tipTextWhenPhotosEmpty detailText:nil buttonTitle:nil buttonAction:nil];
186+
NSString *tipString = self.tipTextWhenPhotosEmpty ? : @"空照片";
187+
[self showEmptyViewWithText:tipString detailText:nil buttonTitle:nil buttonAction:nil];
191188
}
192189
}
193190

194-
- (void)didReceiveMemoryWarning {
195-
[super didReceiveMemoryWarning];
196-
}
197-
198191
#pragma mark - <UITableViewDelegate,UITableViewDataSource>
199192

200193
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@@ -227,10 +220,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
227220

228221
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
229222
if (!_imagePickerViewController) {
230-
if (self.albumViewControllerDelegate && [self.albumViewControllerDelegate respondsToSelector:@selector(imagePickerViewControllerForAlbumViewController:)]) {
231-
_imagePickerViewController = [self.albumViewControllerDelegate imagePickerViewControllerForAlbumViewController:self];
232-
}
223+
_imagePickerViewController = [self.albumViewControllerDelegate imagePickerViewControllerForAlbumViewController:self];
233224
}
225+
NSAssert(_imagePickerViewController, @"self.%@ 必须实现 %@ 并返回一个 %@ 对象", NSStringFromSelector(@selector(albumViewControllerDelegate)), NSStringFromSelector(@selector(imagePickerViewControllerForAlbumViewController:)), NSStringFromClass([QMUIImagePickerViewController class]));
234226
QMUIAssetsGroup *assetsGroup = [_albumsArray objectAtIndex:indexPath.row];
235227
[_imagePickerViewController refreshWithAssetsGroup:assetsGroup];
236228
_imagePickerViewController.title = [assetsGroup name];

QMUIKit/UIComponents/ImagePickerLibrary/QMUIImagePickerPreviewViewController.m

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ - (void)initSubviews {
103103
[self.topToolBarView addSubview:self.checkboxButton];
104104

105105
_progressView = [[QMUIPieProgressView alloc] init];
106+
self.progressView.tintColor = self.toolBarTintColor;
106107
self.progressView.hidden = YES;
107108
[self.topToolBarView addSubview:self.progressView];
108109

@@ -331,17 +332,25 @@ - (void)requestImageForZoomImageView:(QMUIZoomImageView *)zoomImageView withInde
331332
};
332333

333334
if (imageAsset.assetType == QMUIAssetTypeLivePhoto) {
335+
imageView.tag = -1;
334336
imageAsset.requestID = [imageAsset requestLivePhotoWithCompletion:^void(PHLivePhoto *result, NSDictionary *info) {
335-
// 如果是走 PhotoKit 的逻辑,那么这个 block 会被多次调用,并且第一次调用时返回的图片是一张小图,
336-
// 这时需要把图片放大到跟屏幕一样大,避免后面加载大图后图片的显示会有跳动
337-
if (_usePhotoKit && [info[PHLivePhotoInfoIsDegradedKey] boolValue]) {
338-
imageView.contentMode = UIViewContentModeScaleAspectFit;
339-
} else {
340-
imageView.contentMode = UIViewContentModeCenter;
337+
// 这里可能因为 imageView 复用,导致前面的请求得到的结果显示到别的 imageView 上,
338+
// 因此判断如果是新请求(无复用问题)或者是当前的请求才把获得的图片结果展示出来
339+
BOOL isNewRequest = (imageView.tag == -1 && imageAsset.requestID == 0);
340+
BOOL isCurrentRequest = imageView.tag == imageAsset.requestID;
341+
BOOL loadICloudImageFault = !result || info[PHImageErrorKey];
342+
BOOL isDegradedImage = [info[PHImageResultIsDegradedKey] boolValue]; // 是否为低清图
343+
if (!loadICloudImageFault && (isNewRequest || isCurrentRequest)) {
344+
// 如果是走 PhotoKit 的逻辑,那么这个 block 会被多次调用,并且第一次调用时返回的图片是一张小图,
345+
// 这时需要把图片放大到跟屏幕一样大,避免后面加载大图后图片的显示会有跳动
346+
if (isDegradedImage || loadICloudImageFault) {
347+
imageView.contentMode = UIViewContentModeScaleAspectFit;
348+
} else {
349+
imageView.contentMode = UIViewContentModeCenter;
350+
}
351+
imageView.livePhoto = result;
341352
}
342353

343-
imageView.livePhoto = result;
344-
345354
BOOL downlaodSucceed = (result && !info) || (![[info objectForKey:PHLivePhotoInfoCancelledKey] boolValue] && ![info objectForKey:PHLivePhotoInfoErrorKey] && ![[info objectForKey:PHLivePhotoInfoIsDegradedKey] boolValue]);
346355

347356
if (downlaodSucceed) {
@@ -356,18 +365,27 @@ - (void)requestImageForZoomImageView:(QMUIZoomImageView *)zoomImageView withInde
356365
}
357366

358367
} withProgressHandler:phProgressHandler];
368+
imageView.tag = imageAsset.requestID;
359369
} else {
370+
imageView.tag = -1;
360371
imageAsset.requestID = [imageAsset requestPreviewImageWithCompletion:^void(UIImage *result, NSDictionary *info) {
361-
// 如果是走 PhotoKit 的逻辑,那么这个 block 会被多次调用,并且第一次调用时返回的图片是一张小图,
362-
// 这时需要把图片放大到跟屏幕一样大,避免后面加载大图后图片的显示会有跳动
363-
if (_usePhotoKit && [info[PHImageResultIsDegradedKey] boolValue]) {
364-
imageView.contentMode = UIViewContentModeScaleAspectFit;
365-
} else {
366-
imageView.contentMode = UIViewContentModeCenter;
372+
// 这里可能因为 imageView 复用,导致前面的请求得到的结果显示到别的 imageView 上,
373+
// 因此判断如果是新请求(无复用问题)或者是当前的请求才把获得的图片结果展示出来
374+
BOOL isNewRequest = (imageView.tag == -1 && imageAsset.requestID == 0);
375+
BOOL isCurrentRequest = imageView.tag == imageAsset.requestID;
376+
BOOL loadICloudImageFault = !result || info[PHImageErrorKey];
377+
BOOL isDegradedImage = [info[PHImageResultIsDegradedKey] boolValue]; // 是否为低清图
378+
if (!loadICloudImageFault && (isNewRequest || isCurrentRequest)) {
379+
// 如果是走 PhotoKit 的逻辑,那么这个 block 会被多次调用,并且第一次调用时返回的图片是一张小图,
380+
// 这时需要把图片放大到跟屏幕一样大,避免后面加载大图后图片的显示会有跳动
381+
if (isDegradedImage || loadICloudImageFault) {
382+
imageView.contentMode = UIViewContentModeScaleAspectFit;
383+
} else {
384+
imageView.contentMode = UIViewContentModeCenter;
385+
}
386+
imageView.image = result;
367387
}
368388

369-
imageView.image = result;
370-
371389
BOOL downlaodSucceed = (result && !info) || (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]);
372390

373391
if (downlaodSucceed) {
@@ -382,6 +400,7 @@ - (void)requestImageForZoomImageView:(QMUIZoomImageView *)zoomImageView withInde
382400
}
383401

384402
} withProgressHandler:phProgressHandler];
403+
imageView.tag = imageAsset.requestID;
385404
}
386405
}
387406

QMUIKit/UIComponents/ImagePickerLibrary/QMUIImagePickerViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ - (void)handleProgressViewClick:(id)sender {
426426
if (imageAsset.downloadStatus == QMUIAssetDownloadStatusDownloading) {
427427
// 下载过程中点击,取消下载,理论上能点击 progressView 就肯定是下载中,这里只是做个保护
428428
QMUIImagePickerCollectionViewCell *cell = (QMUIImagePickerCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
429-
[[QMUIAssetsManager sharedInstance].phCachingImageManager cancelImageRequest:(int)imageAsset.requestID];
429+
[[QMUIAssetsManager sharedInstance].phCachingImageManager cancelImageRequest:(int32_t)imageAsset.requestID];
430430
QMUILog(@"Cancel download asset image with request ID %@", [NSNumber numberWithInteger:imageAsset.requestID]);
431431
cell.downloadStatus = QMUIAssetDownloadStatusCanceled;
432432
[imageAsset updateDownloadStatusWithDownloadResult:NO];

QMUIKit/UIMainFrame/QMUINavigationController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ - (UIViewController *)popViewControllerAnimated:(BOOL)animated {
155155

156156
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
157157
if (self.isViewControllerTransiting || !viewController) {
158-
NSAssert(NO, @"isViewControllerTransiting = YES, %s, self.viewControllers = %@", __func__, self.viewControllers);
158+
NSAssert(NO, @"%s, isViewControllerTransiting = %@, viewController = %@, self.viewControllers = %@", __func__, StringFromBOOL(self.isViewControllerTransiting), viewController, self.viewControllers);
159159
return;
160160
}
161161

0 commit comments

Comments
 (0)