Commit 21da89e0 authored by banchichen's avatar banchichen

优化滑动帧数,我这里测试iOS89目前帧数平均在58,iOS7平均帧数在57,媲美QQ的图片选择器滑动帧数,比微信的帧数高10左右

parent 7aad4d43
No preview for this file type
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "TZImagePickerController" s.name = "TZImagePickerController"
s.version = "1.3.5" s.version = "1.4.0"
s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video" s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video"
s.homepage = "https://github.com/banchichen/TZImagePickerController" s.homepage = "https://github.com/banchichen/TZImagePickerController"
s.license = "MIT" s.license = "MIT"
s.author = { "banchichen" => "736420282@qq.com" } s.author = { "banchichen" => "736420282@qq.com" }
s.platform = :ios s.platform = :ios
s.ios.deployment_target = "6.0" s.ios.deployment_target = "6.0"
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.3.5" } s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.4.0" }
s.requires_arc = true s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}" s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}" s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// //
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <Photos/Photos.h>
typedef enum : NSUInteger { typedef enum : NSUInteger {
TZAssetCellTypePhoto = 0, TZAssetCellTypePhoto = 0,
...@@ -22,7 +23,8 @@ typedef enum : NSUInteger { ...@@ -22,7 +23,8 @@ typedef enum : NSUInteger {
@property (nonatomic, strong) TZAssetModel *model; @property (nonatomic, strong) TZAssetModel *model;
@property (nonatomic, copy) void (^didSelectPhotoBlock)(BOOL); @property (nonatomic, copy) void (^didSelectPhotoBlock)(BOOL);
@property (nonatomic, assign) TZAssetCellType type; @property (nonatomic, assign) TZAssetCellType type;
@property (nonatomic, copy) NSString *representedAssetIdentifier;
@property (nonatomic, assign) PHImageRequestID imageRequestID;
@end @end
......
...@@ -35,9 +35,29 @@ ...@@ -35,9 +35,29 @@
- (void)setModel:(TZAssetModel *)model { - (void)setModel:(TZAssetModel *)model {
_model = model; _model = model;
[[TZImageManager manager] getPhotoWithAsset:model.asset photoWidth:self.tz_width completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) { if (!iOS8Later) {
self.imageView.image = photo; self.representedAssetIdentifier = [[TZImageManager manager] getAssetIdentifier:model.asset];
}
PHImageRequestID imageRequestID = [[TZImageManager manager] getPhotoWithAsset:model.asset photoWidth:self.tz_width completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
// Set the cell's thumbnail image if it's still showing the same asset.
if (!iOS8Later) {
self.imageView.image = photo; return;
}
if ([self.representedAssetIdentifier isEqualToString:[[TZImageManager manager] getAssetIdentifier:model.asset]]) {
self.imageView.image = photo;
} else {
NSLog(@"this cell is showing other asset");
[[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
}
if (!isDegraded) {
self.imageRequestID = 0;
}
}]; }];
if (imageRequestID && self.imageRequestID && imageRequestID != self.imageRequestID) {
[[PHImageManager defaultManager] cancelImageRequest:self.imageRequestID];
NSLog(@"cancelImageRequest %d",self.imageRequestID);
}
self.imageRequestID = imageRequestID;
self.selectPhotoButton.selected = model.isSelected; self.selectPhotoButton.selected = model.isSelected;
self.selectImageView.image = self.selectPhotoButton.isSelected ? [UIImage imageNamedFromMyBundle:@"photo_sel_photoPickerVc.png"] : [UIImage imageNamedFromMyBundle:@"photo_def_photoPickerVc.png"]; self.selectImageView.image = self.selectPhotoButton.isSelected ? [UIImage imageNamedFromMyBundle:@"photo_sel_photoPickerVc.png"] : [UIImage imageNamedFromMyBundle:@"photo_def_photoPickerVc.png"];
self.type = TZAssetCellTypePhoto; self.type = TZAssetCellTypePhoto;
......
...@@ -33,8 +33,8 @@ ...@@ -33,8 +33,8 @@
/// Get photo 获得照片 /// Get photo 获得照片
- (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *postImage))completion; - (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *postImage))completion;
- (void)getPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion; - (PHImageRequestID)getPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion;
- (void)getPhotoWithAsset:(id)asset photoWidth:(CGFloat)photoWidth completion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion; - (PHImageRequestID)getPhotoWithAsset:(id)asset photoWidth:(CGFloat)photoWidth completion:(void (^)(UIImage *photo,NSDictionary *info,BOOL isDegraded))completion;
- (void)getOriginalPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion; - (void)getOriginalPhotoWithAsset:(id)asset completion:(void (^)(UIImage *photo,NSDictionary *info))completion;
/// Save photo 保存照片 /// Save photo 保存照片
...@@ -52,4 +52,6 @@ ...@@ -52,4 +52,6 @@
/// Judge is a assets array contain the asset 判断一个assets数组是否包含这个asset /// Judge is a assets array contain the asset 判断一个assets数组是否包含这个asset
- (BOOL)isAssetsArray:(NSArray *)assets containAsset:(id)asset; - (BOOL)isAssetsArray:(NSArray *)assets containAsset:(id)asset;
- (NSString *)getAssetIdentifier:(id)asset;
@end @end
...@@ -17,12 +17,21 @@ ...@@ -17,12 +17,21 @@
@implementation TZImageManager @implementation TZImageManager
static CGSize AssetGridThumbnailSize;
+ (instancetype)manager { + (instancetype)manager {
static TZImageManager *manager; static TZImageManager *manager;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
manager = [[self alloc] init]; manager = [[self alloc] init];
manager.cachingImageManager = [[PHCachingImageManager alloc] init]; manager.cachingImageManager = [[PHCachingImageManager alloc] init];
manager.cachingImageManager.allowsCachingHighQualityImages = NO;
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = 4;
CGFloat itemWH = (screenWidth - 2 * margin - 4) / 4 - margin;
AssetGridThumbnailSize = CGSizeMake(itemWH * scale, itemWH * scale);
}); });
return manager; return manager;
} }
...@@ -298,20 +307,25 @@ ...@@ -298,20 +307,25 @@
#pragma mark - Get Photo #pragma mark - Get Photo
/// Get photo 获得照片本身 /// Get photo 获得照片本身
- (void)getPhotoWithAsset:(id)asset completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion { - (PHImageRequestID)getPhotoWithAsset:(id)asset completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion {
[self getPhotoWithAsset:asset photoWidth:[UIScreen mainScreen].bounds.size.width completion:completion]; return [self getPhotoWithAsset:asset photoWidth:[UIScreen mainScreen].bounds.size.width completion:completion];
} }
- (void)getPhotoWithAsset:(id)asset photoWidth:(CGFloat)photoWidth completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion { - (PHImageRequestID)getPhotoWithAsset:(id)asset photoWidth:(CGFloat)photoWidth completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion {
if (photoWidth > 600) photoWidth = 600.0; if (photoWidth > 600) photoWidth = 600.0;
if ([asset isKindOfClass:[PHAsset class]]) { if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = (PHAsset *)asset; CGSize imageSize;
CGFloat aspectRatio = phAsset.pixelWidth / (CGFloat)phAsset.pixelHeight; if (photoWidth < 150) {
CGFloat multiple = [UIScreen mainScreen].scale; imageSize = AssetGridThumbnailSize;
CGFloat pixelWidth = photoWidth * multiple; } else {
CGFloat pixelHeight = pixelWidth / aspectRatio; PHAsset *phAsset = (PHAsset *)asset;
CGFloat aspectRatio = phAsset.pixelWidth / (CGFloat)phAsset.pixelHeight;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(pixelWidth, pixelHeight) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) { CGFloat multiple = [UIScreen mainScreen].scale;
CGFloat pixelWidth = photoWidth * multiple;
CGFloat pixelHeight = pixelWidth / aspectRatio;
imageSize = CGSizeMake(pixelWidth, pixelHeight);
}
PHImageRequestID imageRequestID = [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:imageSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]); BOOL downloadFinined = (![[info objectForKey:PHImageCancelledKey] boolValue] && ![info objectForKey:PHImageErrorKey]);
if (downloadFinined && result) { if (downloadFinined && result) {
result = [self fixOrientation:result]; result = [self fixOrientation:result];
...@@ -323,7 +337,7 @@ ...@@ -323,7 +337,7 @@
option.networkAccessAllowed = YES; option.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { [[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage *resultImage = [UIImage imageWithData:imageData scale:0.1]; UIImage *resultImage = [UIImage imageWithData:imageData scale:0.1];
resultImage = [self scaleImage:resultImage toSize:CGSizeMake(pixelWidth, pixelHeight)]; resultImage = [self scaleImage:resultImage toSize:imageSize];
if (resultImage) { if (resultImage) {
resultImage = [self fixOrientation:resultImage]; resultImage = [self fixOrientation:resultImage];
if (completion) completion(resultImage,info,[[info objectForKey:PHImageResultIsDegradedKey] boolValue]); if (completion) completion(resultImage,info,[[info objectForKey:PHImageResultIsDegradedKey] boolValue]);
...@@ -331,24 +345,30 @@ ...@@ -331,24 +345,30 @@
}]; }];
} }
}]; }];
return imageRequestID;
} else if ([asset isKindOfClass:[ALAsset class]]) { } else if ([asset isKindOfClass:[ALAsset class]]) {
ALAsset *alAsset = (ALAsset *)asset; ALAsset *alAsset = (ALAsset *)asset;
ALAssetRepresentation *assetRep = [alAsset defaultRepresentation]; dispatch_async(dispatch_get_global_queue(0,0), ^{
CGImageRef thumbnailImageRef = alAsset.aspectRatioThumbnail; CGImageRef thumbnailImageRef = alAsset.thumbnail;
UIImage *thumbnailImage = [UIImage imageWithCGImage:thumbnailImageRef scale:1.0 orientation:UIImageOrientationUp]; UIImage *thumbnailImage = [UIImage imageWithCGImage:thumbnailImageRef scale:2.0 orientation:UIImageOrientationUp];
if (completion) completion(thumbnailImage,nil,YES); dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(thumbnailImage,nil,YES);
if (photoWidth == [UIScreen mainScreen].bounds.size.width) {
dispatch_async(dispatch_get_global_queue(0,0), ^{
CGImageRef fullScrennImageRef = [assetRep fullScreenImage];
UIImage *fullScrennImage = [UIImage imageWithCGImage:fullScrennImageRef scale:1.0 orientation:UIImageOrientationUp];
dispatch_async(dispatch_get_main_queue(), ^{ if (photoWidth == [UIScreen mainScreen].bounds.size.width) {
if (completion) completion(fullScrennImage,nil,NO); dispatch_async(dispatch_get_global_queue(0,0), ^{
}); ALAssetRepresentation *assetRep = [alAsset defaultRepresentation];
CGImageRef fullScrennImageRef = [assetRep fullScreenImage];
UIImage *fullScrennImage = [UIImage imageWithCGImage:fullScrennImageRef scale:2.0 orientation:UIImageOrientationUp];
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(fullScrennImage,nil,NO);
});
});
}
}); });
} });
} }
return 0;
} }
- (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *))completion { - (void)getPostImageWithAlbumModel:(TZAlbumModel *)model completion:(void (^)(UIImage *))completion {
...@@ -503,6 +523,17 @@ ...@@ -503,6 +523,17 @@
} }
} }
- (NSString *)getAssetIdentifier:(id)asset {
if (iOS8Later) {
PHAsset *phAsset = (PHAsset *)asset;
return phAsset.localIdentifier;
} else {
ALAsset *alAsset = (ALAsset *)asset;
NSURL *assetUrl = [alAsset valueForProperty:ALAssetPropertyAssetURL];
return assetUrl.absoluteString;
}
}
#pragma mark - Private Method #pragma mark - Private Method
- (TZAlbumModel *)modelWithResult:(id)result name:(NSString *)name{ - (TZAlbumModel *)modelWithResult:(id)result name:(NSString *)name{
......
...@@ -592,8 +592,10 @@ static CGSize AssetGridThumbnailSize; ...@@ -592,8 +592,10 @@ static CGSize AssetGridThumbnailSize;
NSMutableArray *assets = [NSMutableArray arrayWithCapacity:indexPaths.count]; NSMutableArray *assets = [NSMutableArray arrayWithCapacity:indexPaths.count];
for (NSIndexPath *indexPath in indexPaths) { for (NSIndexPath *indexPath in indexPaths) {
TZAssetModel *model = _models[indexPath.item]; if (indexPath.item < _models.count) {
[assets addObject:model.asset]; TZAssetModel *model = _models[indexPath.item];
[assets addObject:model.asset];
}
} }
return assets; return assets;
......
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