Skip to content

Commit

Permalink
增加对PHAsset的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintGao committed Jun 16, 2020
1 parent 5f4165a commit e4467b3
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 27 deletions.
4 changes: 4 additions & 0 deletions GKPhotoBrowser/GKPhoto.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "GKWebImageProtocol.h"
#import "GKPhotoBrowserConfigure.h"

Expand All @@ -27,6 +28,9 @@
/** 图片(静态) */
@property (nonatomic, strong) UIImage *image;

/** 相册图片资源 */
@property (nonatomic, strong) PHAsset *imageAsset;

// imageView对象
@property (nonatomic, strong) UIImageView *imageView;

Expand Down
1 change: 0 additions & 1 deletion GKPhotoBrowser/GKPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ - (void)showFromVC:(UIViewController *)vc {
if (self.showStyle == GKPhotoBrowserShowStylePush) {
[vc.navigationController pushViewController:self animated:YES];
}else {
self.modalPresentationStyle = UIModalPresentationFullScreen;
self.modalPresentationCapturesStatusBarAppearance = YES;
[vc presentViewController:self animated:NO completion:nil];
}
Expand Down
11 changes: 10 additions & 1 deletion GKPhotoBrowser/GKPhotoBrowserConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ NO)

#define kPhotoViewPadding 10

#define kAnimationDuration 0.25f
#define kAnimationDuration 0.3f

#define GKPhotoBrowserSrcName(file) [@"GKPhotoBrowser.bundle" stringByAppendingPathComponent:file]

#define GKPhotoBrowserFrameworkSrcName(file) [@"Frameworks/GKPhotoBrowser.framework/GKPhotoBrowser.bundle" stringByAppendingPathComponent:file]

#define GKPhotoBrowserImage(file) [UIImage imageNamed:GKPhotoBrowserSrcName(file)] ? : [UIImage imageNamed:GKPhotoBrowserFrameworkSrcName(file)]

#define GK_DISPATCH_ASYNC(queue, block)\
if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(queue)) == 0) {\
block();\
} else {\
dispatch_async(queue, block);\
}

#define GK_DISPATCH_ASYNC_MAIN(block) GK_DISPATCH_ASYNC(dispatch_get_main_queue(), block)

// 图片浏览器的显示方式
typedef NS_ENUM(NSUInteger, GKPhotoBrowserShowStyle) {
GKPhotoBrowserShowStyleNone, // 直接显示,默认方式
Expand Down
20 changes: 20 additions & 0 deletions GKPhotoBrowser/GKPhotoManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// GKPhotoManager.h
// GKPhotoBrowserDemo
//
// Created by gaokun on 2020/6/16.
// Copyright © 2020 QuintGao. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Photos/Photos.h>

NS_ASSUME_NONNULL_BEGIN

@interface GKPhotoManager : NSObject

+ (void)loadImageDataWithImageAsset:(PHAsset *)imageAsset completion:(nonnull void(^)(NSData *_Nullable data))completion;

@end

NS_ASSUME_NONNULL_END
28 changes: 28 additions & 0 deletions GKPhotoBrowser/GKPhotoManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GKPhotoManager.m
// GKPhotoBrowserDemo
//
// Created by gaokun on 2020/6/16.
// Copyright © 2020 QuintGao. All rights reserved.
//

#import "GKPhotoManager.h"

@implementation GKPhotoManager

+ (void)loadImageDataWithImageAsset:(PHAsset *)imageAsset completion:(void (^)(NSData * _Nullable))completion {
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.networkAccessAllowed = YES;
options.resizeMode = PHImageRequestOptionsResizeModeNone;
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:imageAsset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
BOOL complete = ![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey] && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue];
if (complete && imageData) {
completion(imageData);
} else {
completion(nil);
}
}];
}

@end
62 changes: 51 additions & 11 deletions GKPhotoBrowser/GKPhotoView.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
//

#import "GKPhotoView.h"
#import "GKPhotoManager.h"

static dispatch_queue_t GKPhotoProcessingQueue(void) {
static dispatch_queue_t queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("com.gk.photobrowser", DISPATCH_QUEUE_CONCURRENT);
});
return queue;
}

@implementation GKScrollView

Expand Down Expand Up @@ -147,17 +157,6 @@ - (void)loadImageWithPhoto:(GKPhoto *)photo isOrigin:(BOOL)isOrigin {
// 每次设置数据时,恢复缩放
[self.scrollView setZoomScale:1.0 animated:NO];

if (photo.image) {
self.imageView.image = photo.image;
[self.loadingView stopLoading];
[self.loadingView hideFailure];
[self.loadingView removeFromSuperview];

[self adjustFrame];

return;
}

// 获取原图的缓存
if ([_imageProtocol imageFromMemoryForURL:photo.originUrl]) {
photo.originFinished = YES;
Expand All @@ -181,6 +180,47 @@ - (void)loadImageWithPhoto:(GKPhoto *)photo isOrigin:(BOOL)isOrigin {
self.imageView.image = placeholderImage;
self.imageView.contentMode = photo.sourceImageView.contentMode;
self.scrollView.scrollEnabled = NO;

if (photo.image) {
self.imageView.image = photo.image;
[self.loadingView stopLoading];
[self.loadingView hideFailure];
[self.loadingView removeFromSuperview];

[self adjustFrame];

return;
}else if (photo.imageAsset) {
[self addSubview:self.loadingView];
[self.loadingView hideFailure];
[self adjustFrame];

if (!photo.failed && !placeholderImage) {
if (isOrigin && self.originLoadStyle != GKPhotoBrowserLoadStyleCustom) {
[self.loadingView startLoading];
}else if (!isOrigin && self.loadStyle != GKPhotoBrowserLoadStyleCustom) {
[self.loadingView startLoading];
}
}

[GKPhotoManager loadImageDataWithImageAsset:photo.imageAsset completion:^(NSData * _Nullable data) {
if (data) {
__weak __typeof(self) wSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong __typeof(wSelf) self = wSelf;
photo.image = [UIImage imageWithData:data];
self.imageView.image = photo.image;
[self.loadingView stopLoading];
[self.loadingView hideFailure];
[self.loadingView removeFromSuperview];
[self adjustFrame];
});
}
}];

return;
}

// 进度条
[self addSubview:self.loadingView];
[self.loadingView hideFailure];
Expand Down
12 changes: 12 additions & 0 deletions GKPhotoBrowserDemo/GKPhotoBrowserDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
6020A14F2456E88C0060C5DB /* GKLoadingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6020A14D2456E88C0060C5DB /* GKLoadingView.m */; };
6020A1542456E9030060C5DB /* GKYYWebImageManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6020A1532456E9030060C5DB /* GKYYWebImageManager.m */; };
602CC2DC21226AD500DE81BF /* GKTopView.m in Sources */ = {isa = PBXBuildFile; fileRef = 602CC2DB21226AD500DE81BF /* GKTopView.m */; };
60318BAC2498A39400ABC67B /* GKPhotoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 60318BAB2498A39400ABC67B /* GKPhotoManager.m */; };
60318BAF2498ACAD00ABC67B /* GKPublishViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 60318BAE2498ACAD00ABC67B /* GKPublishViewController.m */; };
60415CB22121648F00D1D54B /* GKSDAutoLayoutViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 60415CB12121648F00D1D54B /* GKSDAutoLayoutViewController.m */; };
60415CB72121715000D1D54B /* test.txt in Resources */ = {isa = PBXBuildFile; fileRef = 60415CB62121715000D1D54B /* test.txt */; };
60415CBA2121750B00D1D54B /* GKBottomView.m in Sources */ = {isa = PBXBuildFile; fileRef = 60415CB92121750B00D1D54B /* GKBottomView.m */; };
Expand Down Expand Up @@ -96,6 +98,10 @@
6020A1532456E9030060C5DB /* GKYYWebImageManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKYYWebImageManager.m; sourceTree = "<group>"; };
602CC2DA21226AD500DE81BF /* GKTopView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GKTopView.h; sourceTree = "<group>"; };
602CC2DB21226AD500DE81BF /* GKTopView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKTopView.m; sourceTree = "<group>"; };
60318BAA2498A39400ABC67B /* GKPhotoManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GKPhotoManager.h; sourceTree = "<group>"; };
60318BAB2498A39400ABC67B /* GKPhotoManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKPhotoManager.m; sourceTree = "<group>"; };
60318BAD2498ACAD00ABC67B /* GKPublishViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GKPublishViewController.h; sourceTree = "<group>"; };
60318BAE2498ACAD00ABC67B /* GKPublishViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKPublishViewController.m; sourceTree = "<group>"; };
60415CB02121648F00D1D54B /* GKSDAutoLayoutViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GKSDAutoLayoutViewController.h; sourceTree = "<group>"; };
60415CB12121648F00D1D54B /* GKSDAutoLayoutViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKSDAutoLayoutViewController.m; sourceTree = "<group>"; };
60415CB62121715000D1D54B /* test.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = test.txt; sourceTree = "<group>"; };
Expand Down Expand Up @@ -301,6 +307,8 @@
6020A1502456E8AF0060C5DB /* SDWebImage */,
795FA5CB1FF334100057BDB0 /* GKPhotoBrowserConfigure.h */,
795FA5C71FF334100057BDB0 /* GKWebImageProtocol.h */,
60318BAA2498A39400ABC67B /* GKPhotoManager.h */,
60318BAB2498A39400ABC67B /* GKPhotoManager.m */,
795FA5C61FF334100057BDB0 /* GKPhoto.h */,
795FA5CC1FF334100057BDB0 /* GKPhoto.m */,
795FA5C91FF334100057BDB0 /* GKPhotoView.h */,
Expand Down Expand Up @@ -399,6 +407,8 @@
children = (
795FA6021FF338200057BDB0 /* GKTimeLineViewController.h */,
795FA5FD1FF338200057BDB0 /* GKTimeLineViewController.m */,
60318BAD2498ACAD00ABC67B /* GKPublishViewController.h */,
60318BAE2498ACAD00ABC67B /* GKPublishViewController.m */,
795FA5FC1FF338200057BDB0 /* GKTimeLineModel.h */,
795FA5FF1FF338200057BDB0 /* GKTimeLineModel.m */,
795FA6001FF338200057BDB0 /* GKTimeLineViewCell.h */,
Expand Down Expand Up @@ -639,6 +649,7 @@
795FA6121FF338200057BDB0 /* GKToutiaoDetailViewController.m in Sources */,
7927F0D020A97F280017FD5B /* GKTestViewController.m in Sources */,
60415CBA2121750B00D1D54B /* GKBottomView.m in Sources */,
60318BAF2498ACAD00ABC67B /* GKPublishViewController.m in Sources */,
60F9901622715A18008150AE /* GKOriginImageViewController.m in Sources */,
795FA6111FF338200057BDB0 /* GKBaseViewController.m in Sources */,
6020A1542456E9030060C5DB /* GKYYWebImageManager.m in Sources */,
Expand All @@ -657,6 +668,7 @@
60154B5F20D11D6B0000877A /* GKGIFViewController.m in Sources */,
795FA60F1FF338200057BDB0 /* GKPhotosView.m in Sources */,
795FA60E1FF338200057BDB0 /* GKMainViewController.m in Sources */,
60318BAC2498A39400ABC67B /* GKPhotoManager.m in Sources */,
795FA6101FF338200057BDB0 /* GKJianshuViewController.m in Sources */,
795FA6231FF338200057BDB0 /* GKTest02ViewController.m in Sources */,
795FA61D1FF338200057BDB0 /* GKTimeLineViewController.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

@property (nonatomic, strong) NSArray *images;

@property (nonatomic, strong) NSArray *photoImages;

+ (GKPhotosView *)photosViewWithWidth:(CGFloat)width andMargin:(CGFloat)photoMargin;

+ (CGSize)sizeWithCount:(NSInteger)count;
Expand Down
23 changes: 23 additions & 0 deletions GKPhotoBrowserDemo/GKPhotoBrowserDemo/Classes/Main/GKPhotosView.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ - (void)setImages:(NSArray *)images {
}
}

- (void)setPhotoImages:(NSArray *)photoImages {
_photoImages = photoImages;

// 防止出现重用
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

for (NSInteger i = 0; i < photoImages.count; i++) {
UIImageView *imgView = [NSClassFromString(@"SDAnimatedImageView") new];
if (!imgView) {
imgView = [UIImageView new];
}
imgView.contentMode = UIViewContentModeScaleAspectFill;
imgView.clipsToBounds = YES;
imgView.tag = i;
[self addSubview:imgView];

imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)]];

imgView.image = photoImages[i];
}
}

- (UIImage *)cropImage:(UIImage *)image {
CGRect rect;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// GKPublishViewController.h
// GKPhotoBrowserDemo
//
// Created by gaokun on 2020/6/16.
// Copyright © 2020 QuintGao. All rights reserved.
//

#import "GKBaseViewController.h"

NS_ASSUME_NONNULL_BEGIN

@interface GKPublishViewController : GKBaseViewController

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// GKPublishViewController.m
// GKPhotoBrowserDemo
//
// Created by gaokun on 2020/6/16.
// Copyright © 2020 QuintGao. All rights reserved.
//

#import "GKPublishViewController.h"
#import <TZImagePickerController/TZImagePickerController.h>
#import "GKPhotosView.h"
#import "GKPhotoBrowser.h"

@interface GKPublishViewController ()<TZImagePickerControllerDelegate, GKPhotosViewDelegate>

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) GKPhotosView *photoView;

@property (nonatomic, strong) NSArray *assets;

@end

@implementation GKPublishViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.gk_navTitle = @"发布";
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.selectBtn];
self.selectBtn.frame = CGRectMake((KScreenW - 80)/2, 100, 80, 30);

[self.view addSubview:self.photoView];
self.photoView.frame = CGRectMake(0, 150, (kScreenW - 60 - 50 - 20), 0);
}

- (void)selectPhoto {
TZImagePickerController *pickerVC = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
pickerVC.allowTakeVideo = NO;
pickerVC.allowPickingVideo = NO;
pickerVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:pickerVC animated:YES completion:nil];
}

#pragma mark - TZImagePickerControllerDelegate
- (void)tz_imagePickerControllerDidCancel:(TZImagePickerController *)picker {

}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
self.assets = assets;

self.photoView.photoImages = photos;

CGFloat height = [GKPhotosView sizeWithCount:photos.count width:(kScreenW - 60 - 50 - 20) andMargin:5].height;
self.photoView.frame = CGRectMake(0, 150, (kScreenW - 60 - 50 - 20), height);
}

#pragma mark - GKPhotosViewDelegate
- (void)photoTapped:(UIImageView *)imgView {
NSMutableArray *photos = [NSMutableArray new];
[self.assets enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
GKPhoto *photo = [GKPhoto new];
photo.imageAsset = self.assets[idx];
photo.sourceImageView = self.photoView.subviews[idx];
[photos addObject:photo];
}];

GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:imgView.tag];
browser.showStyle = GKPhotoBrowserShowStyleZoom;
browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;
browser.loadStyle = GKPhotoBrowserLoadStyleIndeterminateMask;
[browser showFromVC:self];
}

#pragma mark - 懒加载
- (UIButton *)selectBtn {
if (!_selectBtn) {
_selectBtn = [UIButton new];
[_selectBtn setTitle:@"选择图片" forState:UIControlStateNormal];
_selectBtn.backgroundColor = [UIColor redColor];
[_selectBtn addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
}
return _selectBtn;
}

- (GKPhotosView *)photoView {
if (!_photoView) {
_photoView = [GKPhotosView photosViewWithWidth:(kScreenW - 60 - 50 - 20) andMargin:5];
_photoView.delegate = self;
}
return _photoView;
}

@end
Loading

0 comments on commit e4467b3

Please sign in to comment.