Commit 8aad3746 authored by banchichen's avatar banchichen

修复ipad下内存占用过高的问题

parent 36abbfc3
Pod::Spec.new do |s|
s.name = "TZImagePickerController"
s.version = "1.4.3"
s.version = "1.4.4"
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.4.3" }
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.4.4" }
s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
......
......@@ -20,6 +20,9 @@
@property (nonatomic, assign) BOOL shouldFixOrientation;
/// Default is 540px / 默认540像素宽
@property (nonatomic, assign) CGFloat photoPreviewMaxWidth;
/// Return YES if Authorized 返回YES如果得到了授权
- (BOOL)authorizationStatusAuthorized;
......
......@@ -18,6 +18,8 @@
@implementation TZImageManager
static CGSize AssetGridThumbnailSize;
static CGFloat TZScreenWidth;
static CGFloat TZScreenScale;
+ (instancetype)manager {
static TZImageManager *manager;
......@@ -27,12 +29,15 @@ static CGSize AssetGridThumbnailSize;
manager.cachingImageManager = [[PHCachingImageManager alloc] init];
manager.cachingImageManager.allowsCachingHighQualityImages = NO;
TZScreenWidth = [UIScreen mainScreen].bounds.size.width;
// 测试发现,如果scale在plus真机上取到3.0,内存会增大特别多。故这里写死成2.0
CGFloat scale = 2.0;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
TZScreenScale = 2.0;
if (TZScreenWidth > 600) {
TZScreenScale = 1.0;
}
CGFloat margin = 4;
CGFloat itemWH = (screenWidth - 2 * margin - 4) / 4 - margin;
AssetGridThumbnailSize = CGSizeMake(itemWH * scale, itemWH * scale);
CGFloat itemWH = (TZScreenWidth - 2 * margin - 4) / 4 - margin;
AssetGridThumbnailSize = CGSizeMake(itemWH * TZScreenScale, itemWH * TZScreenScale);
});
return manager;
}
......@@ -309,21 +314,23 @@ static CGSize AssetGridThumbnailSize;
/// Get photo 获得照片本身
- (PHImageRequestID)getPhotoWithAsset:(id)asset completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion {
return [self getPhotoWithAsset:asset photoWidth:[UIScreen mainScreen].bounds.size.width completion:completion];
CGFloat fullScreenWidth = TZScreenWidth;
if (fullScreenWidth > _photoPreviewMaxWidth) {
fullScreenWidth = _photoPreviewMaxWidth;
}
return [self getPhotoWithAsset:asset photoWidth:fullScreenWidth completion:completion];
}
- (PHImageRequestID)getPhotoWithAsset:(id)asset photoWidth:(CGFloat)photoWidth completion:(void (^)(UIImage *, NSDictionary *, BOOL isDegraded))completion {
if (photoWidth > 600) photoWidth = 600.0;
if ([asset isKindOfClass:[PHAsset class]]) {
CGSize imageSize;
if (photoWidth < 150) {
if (photoWidth < TZScreenWidth && photoWidth < _photoPreviewMaxWidth) {
imageSize = AssetGridThumbnailSize;
} else {
PHAsset *phAsset = (PHAsset *)asset;
CGFloat aspectRatio = phAsset.pixelWidth / (CGFloat)phAsset.pixelHeight;
CGFloat multiple = 2.0;
CGFloat pixelWidth = photoWidth * multiple;
CGFloat pixelHeight = pixelWidth / aspectRatio;
CGFloat pixelWidth = photoWidth * TZScreenScale;
CGFloat pixelHeight = photoWidth / aspectRatio;
imageSize = CGSizeMake(pixelWidth, pixelHeight);
}
PHImageRequestID imageRequestID = [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:imageSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
......@@ -355,7 +362,7 @@ static CGSize AssetGridThumbnailSize;
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(thumbnailImage,nil,YES);
if (photoWidth == [UIScreen mainScreen].bounds.size.width) {
if (photoWidth == TZScreenWidth || photoWidth == _photoPreviewMaxWidth) {
dispatch_async(dispatch_get_global_queue(0,0), ^{
ALAssetRepresentation *assetRep = [alAsset defaultRepresentation];
CGImageRef fullScrennImageRef = [assetRep fullScreenImage];
......
......@@ -37,6 +37,9 @@
/// Default is 828px / 默认828像素宽
@property (nonatomic, assign) CGFloat photoWidth;
/// Default is 520px / 默认540像素宽
@property (nonatomic, assign) CGFloat photoPreviewMaxWidth;
/// Default is 15, While fetching photo, HUD will dismiss automatic if timeout;
/// 超时时间,默认为15秒,当取图片时间超过15秒还没有取成功时,会自动dismiss HUD;
@property (nonatomic, assign) NSInteger timeout;
......
......@@ -87,6 +87,7 @@
self.allowPickingImage = YES;
self.timeout = 15;
self.photoWidth = 828.0;
self.photoPreviewMaxWidth = 540;
if (![[TZImageManager manager] authorizationStatusAuthorized]) {
_tipLable = [[UILabel alloc] init];
......@@ -118,6 +119,7 @@
self.timeout = 15;
self.photoWidth = 828.0;
self.maxImagesCount = selectedAssets.count;
self.photoPreviewMaxWidth = 540;
previewVc.photos = [NSMutableArray arrayWithArray:selectedPhotos];
previewVc.currentIndex = index;
......@@ -214,6 +216,16 @@
}
}
- (void)setPhotoPreviewMaxWidth:(CGFloat)photoPreviewMaxWidth {
_photoPreviewMaxWidth = photoPreviewMaxWidth;
if (photoPreviewMaxWidth > 800) {
_photoPreviewMaxWidth = 800;
} else if (photoPreviewMaxWidth < 500) {
_photoPreviewMaxWidth = 500;
}
[TZImageManager manager].photoPreviewMaxWidth = _photoPreviewMaxWidth;
}
- (void)setSelectedAssets:(NSMutableArray *)selectedAssets {
_selectedAssets = selectedAssets;
_selectedModels = [NSMutableArray array];
......
......@@ -124,6 +124,9 @@ static CGSize AssetGridThumbnailSize;
[self scrollCollectionViewToBottom];
// Determine the size of the thumbnails to request from the PHCachingImageManager
CGFloat scale = 2.0;
if ([UIScreen mainScreen].bounds.size.width > 600) {
scale = 1.0;
}
CGSize cellSize = ((UICollectionViewFlowLayout *)_collectionView.collectionViewLayout).itemSize;
AssetGridThumbnailSize = CGSizeMake(cellSize.width * scale, cellSize.height * scale);
}
......
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