Commit 8ddd7b48 authored by banchichen's avatar banchichen

优化图片列表滑动时的卡顿;新增只选视频模式;新增HUD超时时间属性,默认15秒;

parent 4b95bec2
Pod::Spec.new do |s|
s.name = "TZImagePickerController"
s.version = "1.1.3"
s.version = "1.2.0"
s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video"
s.homepage = "https://github.com/banchichen/TZImagePickerController"
s.license = "MIT"
s.author = { "banchichen" => "736420282@qq.com" }
s.platform = :ios
s.ios.deployment_target = "6.0"
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.1.3" }
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.2.0" }
s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/Resource/*.{png,xib,nib}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
......
......@@ -559,6 +559,7 @@
PRODUCT_BUNDLE_IDENTIFIER = tanzhen.TZImagePickerController;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
......@@ -574,6 +575,7 @@
PRODUCT_BUNDLE_IDENTIFIER = tanzhen.TZImagePickerController;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
......
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="14F1509" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
......
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="14F1509" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
......
......@@ -13,18 +13,25 @@
#import "TZImagePickerController.h"
@interface TZAssetCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView; // The photo / 照片
@property (weak, nonatomic) IBOutlet UIImageView *selectImageView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UILabel *timeLength;
@property (weak, nonatomic) UIImageView *imageView; // The photo / 照片
@property (weak, nonatomic) UIImageView *selectImageView;
@property (weak, nonatomic) UIView *bottomView;
@property (weak, nonatomic) UILabel *timeLength;
@property (nonatomic, weak) UIImageView *viewImgView;
@end
@implementation TZAssetCell
// Now we use code to create subViews for improve performance
// 现在我们用代码来创建TZAssetCell和TZAlbumCell的子控件,以提高性能
/*
- (void)awakeFromNib {
self.timeLength.font = [UIFont boldSystemFontOfSize:11];
}
*/
- (void)setModel:(TZAssetModel *)model {
_model = model;
......@@ -55,7 +62,7 @@
}
}
- (IBAction)selectPhotoButtonClick:(UIButton *)sender {
- (void)selectPhotoButtonClick:(UIButton *)sender {
if (self.didSelectPhotoBlock) {
self.didSelectPhotoBlock(sender.isSelected);
}
......@@ -65,18 +72,92 @@
}
}
#pragma mark - Lazy load
- (UIButton *)selectPhotoButton {
if (_selectImageView == nil) {
UIButton *selectImageView = [[UIButton alloc] init];
selectImageView.frame = CGRectMake(self.tz_width - 44, 0, 44, 44);
[selectImageView addTarget:self action:@selector(selectPhotoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:selectImageView];
_selectPhotoButton = selectImageView;
}
return _selectPhotoButton;
}
- (UIImageView *)imageView {
if (_imageView == nil) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, self.tz_width, self.tz_height);
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
[self.contentView addSubview:imageView];
_imageView = imageView;
}
return _imageView;
}
- (UIImageView *)selectImageView {
if (_selectImageView == nil) {
UIImageView *selectImageView = [[UIImageView alloc] init];
selectImageView.frame = CGRectMake(self.tz_width - 27, 0, 27, 27);
[self.contentView addSubview:selectImageView];
_selectImageView = selectImageView;
}
return _selectImageView;
}
- (UIView *)bottomView {
if (_bottomView == nil) {
UIView *bottomView = [[UIView alloc] init];
bottomView.frame = CGRectMake(0, self.tz_height - 17, self.tz_width, 17);
bottomView.backgroundColor = [UIColor blackColor];
bottomView.alpha = 0.8;
[self.contentView addSubview:bottomView];
_bottomView = bottomView;
}
return _bottomView;
}
- (UIImageView *)viewImgView {
if (_viewImgView == nil) {
UIImageView *viewImgView = [[UIImageView alloc] init];
viewImgView.frame = CGRectMake(8, 0, 17, 17);
[viewImgView setImage:[UIImage imageNamed:@"VideoSendIcon"]];
[self.bottomView addSubview:viewImgView];
_viewImgView = viewImgView;
}
return _viewImgView;
}
- (UILabel *)timeLength {
if (_timeLength == nil) {
UILabel *timeLength = [[UILabel alloc] init];
timeLength.font = [UIFont boldSystemFontOfSize:11];
timeLength.frame = CGRectMake(self.viewImgView.tz_right, 0, self.tz_width - self.viewImgView.tz_right - 5, 17);
timeLength.textColor = [UIColor whiteColor];
timeLength.textAlignment = NSTextAlignmentRight;
[self.bottomView addSubview:timeLength];
_timeLength = timeLength;
}
return _timeLength;
}
@end
@interface TZAlbumCell ()
@property (weak, nonatomic) IBOutlet UIImageView *posterImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) UIImageView *posterImageView;
@property (weak, nonatomic) UILabel *titleLable;
@property (weak, nonatomic) UIImageView *arrowImageView;
@end
@implementation TZAlbumCell
/*
- (void)awakeFromNib {
self.posterImageView.clipsToBounds = YES;
}
*/
- (void)setModel:(TZAlbumModel *)model {
_model = model;
......@@ -99,5 +180,43 @@
if (iOS7Later) [super layoutSublayersOfLayer:layer];
}
#pragma mark - Lazy load
- (UIImageView *)posterImageView {
if (_arrowImageView == nil) {
UIImageView *posterImageView = [[UIImageView alloc] init];
posterImageView.contentMode = UIViewContentModeScaleAspectFill;
posterImageView.clipsToBounds = YES;
posterImageView.frame = CGRectMake(0, 0, 70, 70);
[self.contentView addSubview:posterImageView];
_posterImageView = posterImageView;
}
return _posterImageView;
}
- (UILabel *)titleLable {
if (_titleLable == nil) {
UILabel *titleLable = [[UILabel alloc] init];
titleLable.font = [UIFont boldSystemFontOfSize:17];
titleLable.frame = CGRectMake(80, 0, self.tz_width - 80 - 50, self.tz_height);
titleLable.textColor = [UIColor blackColor];
titleLable.textAlignment = NSTextAlignmentLeft;
[self.contentView addSubview:titleLable];
_titleLable = titleLable;
}
return _titleLable;
}
- (UIImageView *)arrowImageView {
if (_arrowImageView == nil) {
UIImageView *arrowImageView = [[UIImageView alloc] init];
CGFloat arrowWH = 15;
arrowImageView.frame = CGRectMake(self.tz_width - arrowWH - 12, 28, arrowWH, arrowWH);
[arrowImageView setImage:[UIImage imageNamed:@"TableViewArrow"]];
[self.contentView addSubview:arrowImageView];
_arrowImageView = arrowImageView;
}
return _arrowImageView;
}
@end
\ No newline at end of file
......@@ -6,6 +6,15 @@
// Copyright © 2015年 谭真. All rights reserved.
//
/*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
最初发现这个问题并修复的是@小鱼周凌宇同学,她的博客地址,http://www.cnblogs.com/coderfish/
表示感谢~
原来xib确实会导致性能问题啊...大家也要注意了...
*/
#import <UIKit/UIKit.h>
#define iOS7Later ([UIDevice currentDevice].systemVersion.floatValue >= 7.0f)
......@@ -22,6 +31,13 @@
/// Default is 9 / 默认最大可选9张图片
@property (nonatomic, assign) NSInteger maxImagesCount;
/// Default is 828px / 默认828像素宽
@property (nonatomic, assign) CGFloat photoWidth;
/// Default is 15, While fetching photo, HUD will dismiss automatic if timeout;
/// 超时时间,默认为15秒,当取图片时间超过15秒还没有取成功时,会自动dismiss HUD;
@property (nonatomic, assign) NSInteger timeout;
/// Default is YES.if set NO, the original photo button will hide. user can't picking original photo.
/// 默认为YES,如果设置为NO,原图按钮将隐藏,用户不能选择发送原图
@property (nonatomic, assign) BOOL allowPickingOriginalPhoto;
......@@ -42,9 +58,9 @@
@property (nonatomic, strong) UIColor *oKButtonTitleColorNormal;
@property (nonatomic, strong) UIColor *oKButtonTitleColorDisabled;
// The picker does not dismiss itself; when client dismisses it these handle will be called.
// The picker should dismiss itself; when it dismissed these handle will be called.
// The second array will be a empty array if user not picking original photo.
// 这个照片选择器不会自己dismiss,用户dismiss这个选择器的时候,会执行下面的handle
// 这个照片选择器会自己dismiss,当选择器dismiss的时候,会执行下面的handle
// 如果用户没有选择发送原图,第二个数组将是空数组
@property (nonatomic, copy) void (^didFinishPickingPhotosHandle)(NSArray<UIImage *> *photos,NSArray *assets);
@property (nonatomic, copy) void (^didFinishPickingPhotosWithInfosHandle)(NSArray<UIImage *> *photos,NSArray *assets,NSArray<NSDictionary *> *infos);
......@@ -62,9 +78,9 @@
@protocol TZImagePickerControllerDelegate <NSObject>
@optional
// The picker does not dismiss itself; the client dismisses it in these callbacks.
// The picker should dismiss itself; when it dismissed these handle will be called.
// Assets will be a empty array if user not picking original photo.
// 这个照片选择器不会自己dismiss,用户dismiss这个选择器的时候,会走下面的回调
// 这个照片选择器会自己dismiss,当选择器dismiss的时候,会执行下面的回调
// 如果用户没有选择发送原图,Assets将是空数组
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets;
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets infos:(NSArray<NSDictionary *> *)infos;
......
......@@ -69,6 +69,7 @@
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[UIApplication sharedApplication].statusBarStyle = _originStatusBarStyle;
[self hideProgressHUD];
}
- (instancetype)initWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<TZImagePickerControllerDelegate>)delegate {
......@@ -82,6 +83,8 @@
_allowPickingOriginalPhoto = YES;
_allowPickingVideo = YES;
_allowPickingImage = YES;
_timeout = 15;
_photoWidth = 828.0;
if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init];
......@@ -162,6 +165,11 @@
}
[_HUDIndicatorView startAnimating];
[[UIApplication sharedApplication].keyWindow addSubview:_progressHUD];
// if over time, dismiss HUD automatic
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.timeout * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hideProgressHUD];
});
}
- (void)hideProgressHUD {
......@@ -171,6 +179,15 @@
}
}
- (void)setTimeout:(NSInteger)timeout {
_timeout = timeout;
if (timeout < 5) {
_timeout = 5;
} else if (_timeout > 60) {
_timeout = 60;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (iOS7Later) viewController.automaticallyAdjustsScrollViewInsets = NO;
if (_timer) { [_timer invalidate]; _timer = nil;}
......@@ -227,7 +244,7 @@
_tableView.tableFooterView = [[UIView alloc] init];
_tableView.dataSource = self;
_tableView.delegate = self;
[_tableView registerNib:[UINib nibWithNibName:@"TZAlbumCell" bundle:nil] forCellReuseIdentifier:@"TZAlbumCell"];
[_tableView registerClass:[TZAlbumCell class] forCellReuseIdentifier:@"TZAlbumCell"];
[self.view addSubview:_tableView];
}];
}
......
......@@ -54,7 +54,7 @@ static CGSize AssetGridThumbnailSize;
[self configCollectionView];
[self configBottomToolBar];
}];
[self resetCachedAssets];
// [self resetCachedAssets];
}
- (void)configCollectionView {
......@@ -75,7 +75,7 @@ static CGSize AssetGridThumbnailSize;
_collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -2);
_collectionView.contentSize = CGSizeMake(self.view.tz_width, ((_model.count + 3) / 4) * self.view.tz_width);
[self.view addSubview:_collectionView];
[_collectionView registerNib:[UINib nibWithNibName:@"TZAssetCell" bundle:nil] forCellWithReuseIdentifier:@"TZAssetCell"];
[_collectionView registerClass:[TZAssetCell class] forCellWithReuseIdentifier:@"TZAssetCell"];
}
- (void)viewWillAppear:(BOOL)animated {
......@@ -209,12 +209,15 @@ static CGSize AssetGridThumbnailSize;
NSMutableArray *infoArr = [NSMutableArray array];
for (NSInteger i = 0; i < _selectedPhotoArr.count; i++) { [photos addObject:@1];[assets addObject:@1];[infoArr addObject:@1]; }
[TZImageManager manager].shouldFixOrientation = YES;
// [TZImageManager manager].shouldFixOrientation = YES;
for (NSInteger i = 0; i < _selectedPhotoArr.count; i++) {
TZAssetModel *model = _selectedPhotoArr[i];
[[TZImageManager manager] getPhotoWithAsset:model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
if (isDegraded) return;
if (photo) [photos replaceObjectAtIndex:i withObject:photo];
if (photo) {
photo = [self scaleImage:photo toSize:CGSizeMake(imagePickerVc.photoWidth, imagePickerVc.photoWidth * photo.size.height / photo.size.width)];
[photos replaceObjectAtIndex:i withObject:photo];
}
if (info) [infoArr replaceObjectAtIndex:i withObject:info];
if (_isSelectOriginalPhoto) [assets replaceObjectAtIndex:i withObject:model.asset];
......@@ -342,6 +345,15 @@ static CGSize AssetGridThumbnailSize;
}];
}
/// Scale image / 缩放图片
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - Asset Caching
- (void)resetCachedAssets {
......
......@@ -66,7 +66,7 @@
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == _selectedPhotos.count) [self pickPhotoButtonClick:nil];
if (indexPath.row == _selectedPhotos.count) { [self pickPhotoButtonClick:nil];}
}
#pragma mark Click Event
......@@ -86,10 +86,10 @@
// imagePickerVc.oKButtonTitleColorDisabled = [UIColor lightGrayColor];
// imagePickerVc.oKButtonTitleColorNormal = [UIColor greenColor];
// Set allow picking video & originalPhoto or not
// 设置是否可以选择视频/原图
// imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.allowPickingImage = NO;
// Set allow picking video & photo & originalPhoto or not
// 设置是否可以选择视频/图片/原图
// imagePickerVc.allowPickingVideo = NO;
// imagePickerVc.allowPickingImage = NO;
// imagePickerVc.allowPickingOriginalPhoto = NO;
[self presentViewController:imagePickerVc animated:YES completion:nil];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment