/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "UIView+WebCache.h"
#if SD_UIKIT || SD_MAC
#import "objc/runtime.h"
#import "UIView+WebCacheOperation.h"
NSString*constSDWebImageInternalSetImageInGlobalQueueKey =@"setImageInGlobalQueue";
staticcharimageURLKey;
#if SD_UIKIT
staticcharTAG_ACTIVITY_INDICATOR;
staticcharTAG_ACTIVITY_STYLE;
#endif
staticcharTAG_ACTIVITY_SHOW;
@implementationUIView (WebCache)
- (nullableNSURL*)sd_imageURL {
return objc_getAssociatedObject(self, &imageURLKey);
}
- (void)sd_internalSetImageWithURL:(nullableNSURL*)url
placeholderImage:(nullableUIImage*)placeholder
options:(SDWebImageOptions)options
operationKey:(nullableNSString*)operationKey
setImageBlock:(nullableSDSetImageBlock)setImageBlock
progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock
completed:(nullableSDExternalCompletionBlock)completedBlock {
return [self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options operationKey:operationKey setImageBlock:setImageBlock progress:progressBlock completed:completedBlock context:nil];
}
- (void)sd_internalSetImageWithURL:(nullableNSURL*)url
placeholderImage:(nullableUIImage*)placeholder
options:(SDWebImageOptions)options
operationKey:(nullableNSString*)operationKey
setImageBlock:(nullableSDSetImageBlock)setImageBlock
progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock
completed:(nullableSDExternalCompletionBlock)completedBlock
context:(nullableNSDictionary*)context {
NSString*validOperationKey = operationKey ?:NSStringFromClass([selfclass]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (!(options & SDWebImageDelayPlaceholder)) {
dispatch_main_async_safe(^{
[selfsd_setImage:placeholder imageData:nilbasedOnClassOrViaCustomSetImageBlock:setImageBlock];
});
}
if(url) {
// check if activityView is enabled or not
if ([self sd_showActivityIndicatorView]) {
[self sd_addActivityIndicator];
}
__weak __typeof(self)wself = self;
id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
__strong__typeof(wself) sself = wself;
[sselfsd_removeActivityIndicator];
if(!sself) {return; }
BOOLshouldCallCompletedBlock = finished || (options &SDWebImageAvoidAutoSetImage);
BOOLshouldNotSetImage = ((image && (options &SDWebImageAvoidAutoSetImage)) ||
(!image && !(options &SDWebImageDelayPlaceholder)));
SDWebImageNoParamsBlockcallCompletedBlockClojure = ^{
if(!sself) {return; }
if(!shouldNotSetImage) {
[sselfsd_setNeedsLayout];
}
if(completedBlock && shouldCallCompletedBlock) {
completedBlock(image, error, cacheType, url);
}
};
// case 1a: we got an image, but the SDWebImageAvoidAutoSetImage flag is set
// OR
// case 1b: we got no image and the SDWebImageDelayPlaceholder is not set
if(shouldNotSetImage) {
dispatch_main_async_safe(callCompletedBlockClojure);
return;
}
UIImage*targetImage =nil;
NSData*targetData =nil;
if(image) {
// case 2a: we got an image and the SDWebImageAvoidAutoSetImage is not set
targetImage = image;
targetData = data;
}else if (options & SDWebImageDelayPlaceholder) {
// case 2b: we got no image and the SDWebImageDelayPlaceholder flag is set
targetImage = placeholder;
targetData =nil;
}
BOOLshouldUseGlobalQueue =NO;
if (context && [context valueForKey:SDWebImageInternalSetImageInGlobalQueueKey]) {
shouldUseGlobalQueue = [[contextvalueForKey:SDWebImageInternalSetImageInGlobalQueueKey] boolValue];
}
dispatch_queue_t targetQueue = shouldUseGlobalQueue ? dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) : dispatch_get_main_queue();
dispatch_queue_async_safe(targetQueue, ^{
[sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock];
dispatch_main_async_safe(callCompletedBlockClojure);
});
}];
[selfsd_setImageLoadOperation:operationforKey:validOperationKey];
}else{
dispatch_main_async_safe(^{
[selfsd_removeActivityIndicator];
if(completedBlock) {
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1userInfo:@{NSLocalizedDescriptionKey :@"Trying to load a nil url"}];
completedBlock(nil, error, SDImageCacheTypeNone, url);
}
});
}
}
/**处理分辨率大的问题*/
- (UIImage*)compressResolvingImage:(UIImage*)orginImage
{
if(orginImage ==nil)returnnil;
if(orginImage.size.width > [UIScreen mainScreen].bounds.size.width || orginImage.size.height >[UIScreen mainScreen].bounds.size.height){
CGSizeorginSize = orginImage.size;
if(orginSize.width >[UIScreen mainScreen].bounds.size.width){
orginSize.height=1.0*[UIScreenmainScreen].bounds.size.width*orginSize.height/orginSize.width;
orginSize.width = [UIScreen mainScreen].bounds.size.width;
}else if (orginSize.height >[UIScreen mainScreen].bounds.size.height){
orginSize.width = 1.0*[UIScreen mainScreen].bounds.size.width/orginSize.height;
orginSize.height = [UIScreen mainScreen].bounds.size.height;
}
return [self imageWithImage:orginImage scaledToSize:orginSize];
}else{
returnorginImage;
}
}
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext(newSize);
[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIImage imageWithData:UIImageJPEGRepresentation(newImage, 0.8)];
}
- (void)sd_cancelCurrentImageLoad {
[self sd_cancelImageLoadOperationWithKey:NSStringFromClass([self class])];
}
- (void)sd_setImage:(UIImage*)image imageData:(NSData*)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock {
if(setImageBlock) {
setImageBlock(image, imageData);
return;
}
#if SD_UIKIT || SD_MAC
if ([self isKindOfClass:[UIImageView class]]) {
UIImageView*imageView = (UIImageView*)self;
UIImage*resultImage = [selfcompressResolvingImage:image];
imageView.image= resultImage;
}
#endif
#if SD_UIKIT
if ([self isKindOfClass:[UIButton class]]) {
UIButton*button = (UIButton*)self;
[buttonsetImage:image forState:UIControlStateNormal];
}
#endif
}
- (void)sd_setNeedsLayout {
#if SD_UIKIT
[self setNeedsLayout];
#elif SD_MAC
[selfsetNeedsLayout:YES];
#endif
}
#pragma mark - Activity indicator
#pragma mark -
#if SD_UIKIT
- (UIActivityIndicatorView*)activityIndicator {
return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &TAG_ACTIVITY_INDICATOR);
}
- (void)setActivityIndicator:(UIActivityIndicatorView*)activityIndicator {
objc_setAssociatedObject(self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN);
}
#endif
- (void)sd_setShowActivityIndicatorView:(BOOL)show {
objc_setAssociatedObject(self, &TAG_ACTIVITY_SHOW, @(show), OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)sd_showActivityIndicatorView {
return [objc_getAssociatedObject(self, &TAG_ACTIVITY_SHOW) boolValue];
}
#if SD_UIKIT
- (void)sd_setIndicatorStyle:(UIActivityIndicatorViewStyle)style{
objc_setAssociatedObject(self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt:style], OBJC_ASSOCIATION_RETAIN);
}
- (int)sd_getIndicatorStyle{
return [objc_getAssociatedObject(self, &TAG_ACTIVITY_STYLE) intValue];
}
#endif
- (void)sd_addActivityIndicator {
#if SD_UIKIT
dispatch_main_async_safe(^{
if(!self.activityIndicator) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:[selfsd_getIndicatorStyle]];
self.activityIndicator.translatesAutoresizingMaskIntoConstraints =NO;
[selfaddSubview:self.activityIndicator];
[selfaddConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];
[selfaddConstraint:[NSLayoutConstraint constraintWithItem:self.activityIndicator
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];
}
[self.activityIndicator startAnimating];
});
#endif
}
- (void)sd_removeActivityIndicator {
#if SD_UIKIT
dispatch_main_async_safe(^{
if(self.activityIndicator) {
[self.activityIndicator removeFromSuperview];
self.activityIndicator =nil;
}
});
#endif
}
@end
#endif
1.列表上高清图片的话上面的文件粘到SdwebImage 里面.的UIView+WebCache
2.图片过大 10几兆 如果单个页面单个图片会爆炸, 那么使用下面的可以,列表的话太大
// [SDImageCache sharedImageCache].config.shouldDecompressImages = YES;
// [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:YES];
// [SDImageCache sharedImageCache].config.shouldDecompressImages = NO;
// [[SDWebImageDownloader sharedDownloader] setShouldDecompressImages:NO];
3.下面的是是否缓存到内存里面自己想要知道就可以去调试或者看看人家代码
[SDImageCache sharedImageCache].config.shouldCacheImagesInMemory = NO;
仅做笔记 ~
网友评论