Skip to content

Commit

Permalink
优化原图功能
Browse files Browse the repository at this point in the history
  • Loading branch information
longitachi committed Jun 22, 2017
1 parent 0459338 commit 59c0461
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
5 changes: 5 additions & 0 deletions PhotoBrowser/ZLCollectionCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ - (void)restartCapture

- (void)startCapture
{
if (![UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
return;
}

if (self.session && [self.session isRunning]) {
return;
}
Expand Down
10 changes: 6 additions & 4 deletions PhotoBrowser/ZLForceTouchPreviewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (void)loadNormalImage
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGSize size = [self getSize];
imageView.frame = (CGRect){{0, 0}, [self getSize]};
imageView.frame = (CGRect){CGPointZero, [self getSize]};
[self.view addSubview:imageView];

[ZLPhotoManager requestImageForAsset:self.model.asset size:CGSizeMake(size.width*2, size.height*2) completion:^(UIImage *img, NSDictionary *info) {
Expand All @@ -73,7 +73,7 @@ - (void)loadGifImage
{
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = (CGRect){{0, 0}, [self getSize]};
imageView.frame = (CGRect){CGPointZero, [self getSize]};
[self.view addSubview:imageView];

[ZLPhotoManager requestOriginalImageDataForAsset:self.model.asset completion:^(NSData *data, NSDictionary *info) {
Expand All @@ -85,7 +85,7 @@ - (void)loadLivePhoto
{
PHLivePhotoView *lpView = [[PHLivePhotoView alloc] init];
lpView.contentMode = UIViewContentModeScaleAspectFit;
lpView.frame = (CGRect){{0, 0}, [self getSize]};
lpView.frame = (CGRect){CGPointZero, [self getSize]};
[self.view addSubview:lpView];

[ZLPhotoManager requestLivePhotoForAsset:self.model.asset completion:^(PHLivePhoto *lv, NSDictionary *info) {
Expand All @@ -97,7 +97,7 @@ - (void)loadLivePhoto
- (void)loadVideo
{
AVPlayerLayer *playLayer = [[AVPlayerLayer alloc] init];
playLayer.frame = (CGRect){{0, 0}, [self getSize]};
playLayer.frame = (CGRect){CGPointZero, [self getSize]};
[self.view.layer addSublayer:playLayer];

[ZLPhotoManager requestVideoForAsset:self.model.asset completion:^(AVPlayerItem *item, NSDictionary *info) {
Expand All @@ -113,6 +113,8 @@ - (CGSize)getSize
{
CGFloat w = MIN(self.model.asset.pixelWidth, kViewWidth);
CGFloat h = w * self.model.asset.pixelHeight / self.model.asset.pixelWidth;
if (isnan(h)) return CGSizeZero;

if (h > kViewHeight) {
h = kViewHeight;
w = h * self.model.asset.pixelWidth / self.model.asset.pixelHeight;
Expand Down
8 changes: 4 additions & 4 deletions PhotoBrowser/ZLPhotoActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) NSMutableArray<PHAsset *> *arrSelectedAssets;

/**选择照片回调,回调解析好的图片、对应的asset对象、是否原图*/
@property (nonatomic, copy) void (^selectImageBlock)(NSArray<UIImage *> *, NSArray<PHAsset *> *, BOOL);
@property (nonatomic, copy) void (^selectImageBlock)(NSArray<UIImage *> *images, NSArray<PHAsset *> *assets, BOOL isOriginal);

/**选择gif照片回调,回调解析好的gif图片、对应的asset对象*/
@property (nonatomic, copy) void (^selectGifBlock)(UIImage *, PHAsset *);
@property (nonatomic, copy) void (^selectGifBlock)(UIImage *gif, PHAsset *asset);

/**选择live photo照片回调,回调解析好的live photo图片、对应的asset对象*/
@property (nonatomic, copy) void (^selectLivePhotoBlock)(UIImage *, PHAsset *);
@property (nonatomic, copy) void (^selectLivePhotoBlock)(UIImage *livePhoto, PHAsset *asset);

/**选择视频回调,回调第一帧封面图片、对应的asset对象,对应的AVPlayerItem对象*/
@property (nonatomic, copy) void (^selectVideoBlock)(UIImage *, PHAsset *);
@property (nonatomic, copy) void (^selectVideoBlock)(UIImage *cover, PHAsset *asset);

- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;

Expand Down
8 changes: 5 additions & 3 deletions PhotoBrowser/ZLPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ - (void)viewDidLoad {
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
weakify(self);
[ZLPhotoManager getPhotoAblumList:nav.allowSelectVideo allowSelectImage:nav.allowSelectImage complete:^(NSArray<ZLAlbumListModel *> *albums) {
self.arrayDataSources = [NSMutableArray arrayWithArray:albums];
strongify(weakSelf);
strongSelf.arrayDataSources = [NSMutableArray arrayWithArray:albums];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[strongSelf.tableView reloadData];
});
}];
});
Expand Down
30 changes: 14 additions & 16 deletions PhotoBrowser/ZLShowBigImgViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ - (void)initBottomView
[_btnOriginalPhoto setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
[_btnOriginalPhoto addTarget:self action:@selector(btnOriginalImage_Click:) forControlEvents:UIControlEventTouchUpInside];
_btnOriginalPhoto.selected = nav.isSelectOriginalPhoto;
if (nav.arrSelectedModels.count > 0) {
[self getPhotosBytes];
}
[self getPhotosBytes];
[_bottomView addSubview:_btnOriginalPhoto];

self.labPhotosBytes = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_btnOriginalPhoto.frame)+5, 7, 80, 30)];
Expand Down Expand Up @@ -185,11 +183,12 @@ - (void)btnOriginalImage_Click:(UIButton *)btn
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
nav.isSelectOriginalPhoto = btn.selected = !btn.selected;
if (btn.selected) {
ZLPhotoModel *m = self.models[_currentPage-1];
if (!m.isSelected) {
[self navRightBtn_Click:_navRightBtn];
} else {
[self getPhotosBytes];
[self getPhotosBytes];
if (!_navRightBtn.isSelected) {
if (nav.showSelectBtn &&
nav.arrSelectedModels.count < nav.maxSelectCount) {
[self navRightBtn_Click:_navRightBtn];
}
}
} else {
self.labPhotosBytes.text = nil;
Expand Down Expand Up @@ -252,7 +251,6 @@ - (void)navRightBtn_Click:(UIButton *)btn
[self.arrSelPhotos addObject:_arrSelPhotosBackup[_currentPage-1]];
[_arrSelAssets addObject:_arrSelAssetsBackup[_currentPage-1]];
}
[self getPhotosBytes];
} else {
//移除
model.isSelected = NO;
Expand Down Expand Up @@ -294,9 +292,11 @@ - (void)getPhotosBytes
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
if (!nav.isSelectOriginalPhoto) return;

if (nav.arrSelectedModels.count > 0) {
NSArray *arr = nav.showSelectBtn?nav.arrSelectedModels:@[self.models[_currentPage-1]];

if (arr.count) {
weakify(self);
[ZLPhotoManager getPhotosBytesWithArray:nav.arrSelectedModels completion:^(NSString *photosBytes) {
[ZLPhotoManager getPhotosBytesWithArray:arr completion:^(NSString *photosBytes) {
strongify(weakSelf);
strongSelf.labPhotosBytes.text = [NSString stringWithFormat:@"(%@)", photosBytes];
}];
Expand Down Expand Up @@ -344,11 +344,6 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol
[(ZLBigImageCell *)cell resetCellStatus];
}

//- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
//{
// [(ZLBigImageCell *)cell resetCellStatus];
//}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZLBigImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLBigImageCell" forIndexPath:indexPath];
Expand Down Expand Up @@ -383,6 +378,9 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
self.title = [NSString stringWithFormat:@"%ld/%ld", _currentPage, self.models.count];
ZLPhotoModel *model = self.models[_currentPage-1];
_navRightBtn.selected = model.isSelected;
//单选模式下获取当前图片大小
ZLImageNavigationController *nav = (ZLImageNavigationController *)self.navigationController;
if (!nav.showSelectBtn) [self getPhotosBytes];
}
}

Expand Down
9 changes: 3 additions & 6 deletions PhotoBrowser/ZLThumbnailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
strongify(weakSelf);
__strong typeof(weakCell) strongCell = weakCell;

//TODO: 在模拟器上运行如果做了强弱转换,会导致预览已选择照片时候界面卡顿假死。暂时这样判断,不影响真机下运行
#if TARGET_IPHONE_SIMULATOR
ZLImageNavigationController *weakNav = nav;
#else
ZLImageNavigationController *weakNav = (ZLImageNavigationController *)strongSelf.navigationController;
#endif
if (!selected) {
//选中
if (weakNav.arrSelectedModels.count >= weakNav.maxSelectCount) {
Expand Down Expand Up @@ -545,7 +540,9 @@ - (CGSize)getSize:(ZLPhotoModel *)model
{
CGFloat w = MIN(model.asset.pixelWidth, kViewWidth);
CGFloat h = w * model.asset.pixelHeight / model.asset.pixelWidth;
if (h > kViewHeight) {
if (isnan(h)) return CGSizeZero;

if (h > kViewHeight || isnan(h)) {
h = kViewHeight;
w = h * model.asset.pixelWidth / model.asset.pixelHeight;
}
Expand Down
2 changes: 1 addition & 1 deletion ZLPhotoBrowser.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ Pod::Spec.new do |s|
s.source_files = 'PhotoBrowser/*.{h,m}'
s.resources = 'PhotoBrowser/resource/*.{png,xib,nib,bundle}'
s.requires_arc = true
s.frameworks = 'UIKit','Photos'
s.frameworks = 'UIKit','Photos','PhotosUI'
end

0 comments on commit 59c0461

Please sign in to comment.