加载系统相册数据的时候是使用的URL的形式来初始化数据的,这没什么问题,问题就出在了,没有区分是缩略图还是全屏图片啊.(MWPhotoBrowser图片的显示是区分缩略图显示还是全屏图片显示的,但是使用的图片都是全屏图片).源码里面默认都是加载的全屏图片,假如我要显示缩略图屏图片的时候给你来自一张全屏图片,而我实际只需要一张小小的缩略图,这不是白白浪费资源吗, 所以决定动手改造一下,我们首先来看看他的初始化方法,就是下面两个方法.貌似看不出什么问题.接着往下看..
// 这里没有区分是否为缩略图
+ (MWPhoto *)photoWithURL:(NSURL *)url {
return [[MWPhoto alloc] initWithURL:url];
}
// 这里也没有区分是否为缩略图
- (id)initWithURL:(NSURL *)url {
if ((self = [super init])) {
self.photoURL = url;
}
return self;
}
看看它是怎么样加载图片的,首先来看看它是怎么加载系统相册里面的图片的,我们找到performLoadUnderlyingImageAndNotify这个方法,里面的代码
if ([[[_photoURL scheme] lowercaseString] isEqualToString:@”assets-library”]) 这一句就是判断是否为系统相册里面的图片. 如果是系统相册图片执行if里面的方法,这里貌似也没什么问题,我们接着往下看.来看看if之后调用的方法._performLoadUnderlyingImageAndNotifyWithAssetsLibraryURL
// Set the underlyingImage
- (void)performLoadUnderlyingImageAndNotify {
// Get underlying image
if (_image) {
// We have UIImage!
self.underlyingImage = _image;
[self imageLoadingComplete];
} else if (_photoURL) {
// Check what type of url it is
if ([[[_photoURL scheme] lowercaseString] isEqualToString:@"assets-library"]) {
// Load from assets library
[self _performLoadUnderlyingImageAndNotifyWithAssetsLibraryURL: _photoURL];
} else if ([_photoURL isFileReferenceURL]) {
// Load from local file async
[self _performLoadUnderlyingImageAndNotifyWithLocalFileURL: _photoURL];
} else {
// Load async from web (using SDWebImage)
[self _performLoadUnderlyingImageAndNotifyWithWebURL: _photoURL];
}
} else if (_asset) {
// Load from photos asset
[self _performLoadUnderlyingImageAndNotifyWithAsset: _asset targetSize:_assetTargetSize];
} else {
// Image is empty
[self imageLoadingComplete];
}
}
接下来就看到 if 语句里面调用的加载图片的方法.
// Load from asset library async
- (void)_performLoadUnderlyingImageAndNotifyWithAssetsLibraryURL:(NSURL *)url {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
@try {
ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
resultBlock:^(ALAsset *asset){
CGImageRef iref = [[asset defaultRepresentation] fullScreenImage];
if (iref) {
self.underlyingImage = [UIImage imageWithCGImage:iref];
}
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
failureBlock:^(NSError *error) {
self.underlyingImage = nil;
MWLog(@"Photo from asset library error: %@",error);
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}];
} @catch (NSException *e) {
MWLog(@"Photo from asset library error: %@", e);
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
}
});
}
看到了吗,里面有下面这一句,这一句是将全屏图片加载出来,然后封装成UIImage. 问题就出在这里.
CGImageRef iref = [[asset defaultRepresentation] fullScreenImage];
这一句没有区分是否为缩略图,假如我进入的是图片列表,需要加载缩略图,但是真是情况是加载全屏图片,这样会造成很大的资源浪费,而且会拖慢运行速度,所以解决办法是加一个属性,来区分是否为缩略图,来分别加载不同大小的图片
首先在 MWPhoto.h 文件中添加属性
@property (nonatomic) BOOL isThumb; //是否为缩略图
然后再把刚才那一句改一下改成下面这样
CGImageRef iref = self.isThumb? [asset thumbnail]:[[asset defaultRepresentation] fullScreenImage];
这样就大功告成了. 再也不用担心加载系统相册卡顿的问题了…
所以有了下面的改造
MWPhoto.h 添加初始化方法,方法参数里面指定是否为缩略图
@property (nonatomic, strong) ALAsset *alAsset;
+ (MWPhoto *)photoWithALAsset:(ALAsset *)url isThumb:(BOOL) isThumb;
- (id)initWithALAsset:(ALAsset *)asset isThumb:(BOOL) isThumb;
MWPhoto.m 添加方法的实现
+(MWPhoto *)photoWithALAsset:(ALAsset *)asset isThumb:(BOOL)isThumb{
return [[MWPhoto alloc]initWithALAsset:asset isThumb:isThumb];
}
-(id)initWithALAsset:(ALAsset *)asset isThumb:(BOOL)isThumb{
if ((self = [super init])) {
self.alAsset = asset;
self.isThumb = isThumb;
}
return self;
}
然后改造一下MWPhoto.m中的performLoadUnderlyingImageAndNotify方法
添加 if(_alAsset){// 添加是否为系统图片的判断,然后判断中调用
_performLoadUnderlyingImageAndNotifyWithAssetsLibraryAlAsset 方法
// Set the underlyingImage
- (void)performLoadUnderlyingImageAndNotify {
// Get underlying image
if (_image) {
// We have UIImage!
self.underlyingImage = _image;
[self imageLoadingComplete];
} else if (_photoURL) {
// Check what type of url it is
if ([[[_photoURL scheme] lowercaseString] isEqualToString:@"assets-library"]) {
// Load from assets library
[self _performLoadUnderlyingImageAndNotifyWithAssetsLibraryURL: _photoURL];
} else if ([_photoURL isFileReferenceURL]) {
// Load from local file async
[self _performLoadUnderlyingImageAndNotifyWithLocalFileURL: _photoURL];
} else {
// Load async from web (using SDWebImage)
[self _performLoadUnderlyingImageAndNotifyWithWebURL: _photoURL];
}
} else if(_alAsset){// 添加是否为系统图片的判断
// Load from photos asset
[self _performLoadUnderlyingImageAndNotifyWithAssetsLibraryAlAsset: _alAsset];
}else if (_asset) {
// Load from photos asset
[self _performLoadUnderlyingImageAndNotifyWithAsset: _asset targetSize:_assetTargetSize];
} else {
// Image is empty
[self imageLoadingComplete];
}
}
方法实现
// Load from asset library async
- (void)_performLoadUnderlyingImageAndNotifyWithAssetsLibraryAlAsset:(ALAsset *) asset {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@autoreleasepool {
@try {
CGImageRef iref = self.isThumb? [asset thumbnail]:[[asset defaultRepresentation] fullScreenImage];
if (iref) {
self.underlyingImage = [UIImage imageWithCGImage:iref];
}
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
} @catch (NSException *e) {
MWLog(@"Photo from asset library error: %@", e);
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
}
});
}
这次又进一步的优化了MWPhotoBrowser的加载性能, 还有MWPhotoBrowser选择图片时候, 会误选择图片问题的解决方案会在下一篇博客中继续讲解…
注: 无选择问题:
假设我点中一张图片然后向上或者向下滑动,这时候有可能会将这张图片勾选上, 假设我在快速滑动的时候,很容易误选择了不该选择的图片(比如)你懂得…