Initial commit

This commit is contained in:
ifish
2017-08-15 16:59:01 +08:00
commit bdc83e07ef
5766 changed files with 423223 additions and 0 deletions
@@ -0,0 +1,30 @@
//
// UMComGridView.h
// UMCommunity
//
// Created by luyiyuan on 14/8/27.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "UMComGridViewerController.h"
@interface UMComGridView : UIView
@property (nonatomic, strong) void (^TapInImage)(UMComGridViewerController *viewerViewController, UIImageView *imageView);
- (id)initWithArray:(NSArray *)array placeholder:(UIImage *)placeholder cellPad:(NSUInteger)cellPad;
- (void)setImages:(NSArray *)images placeholder:(UIImage *)placeholder cellPad:(NSInteger)cellPad;
- (void)setArray:(NSArray *)array;
- (void)setPresentFatherViewController:(UIViewController *)viewController;
//默认一周(60*60*24*7
- (void)setCacheSecondes:(NSTimeInterval)secondes;
- (void)startDownload;
@end
@@ -0,0 +1,236 @@
//
// UMComGridView.m
// UMCommunity
//
// Created by luyiyuan on 14/8/27.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import "UMComGridView.h"
#import "UMComImageView.h"
#import "math.h"
#import "UMComShowToast.h"
#import <UMComFoundation/UMUtils.h>
#import <UMComDataStorage/UMComImageUrl.h>
#define HTTP_PREFIX @"http://"
#define IMAGE_CELL_WIDTH 75
#define A_WEEK_SECONDES (60*60*24*7)
static inline CGRect getImageViewRect(NSUInteger index,NSInteger deltaWidth, NSUInteger cellPad)
{
CGFloat originX = 0;
if (index%3 != 0) {
originX = (cellPad+deltaWidth)*(index%3);
}
return CGRectMake(originX, (cellPad+deltaWidth)*(index/3), deltaWidth, deltaWidth);
}
static inline CGSize getGridViewSize(NSArray *array,NSUInteger cellPad,CGFloat imageWidth)
{
CGSize size;
size.width = [array count]/3 > 0 ? (cellPad+imageWidth) * 3 + cellPad : (cellPad+imageWidth)*([array count]%3)+cellPad;
size.height = (cellPad+imageWidth)*(([array count] - 1)/3 + 1) + cellPad;
return size;
}
@interface UMComGridView ()
@property (nonatomic) NSUInteger cellPad;
@property (nonatomic,strong) UIImage *placeholder;
@property (nonatomic,strong) NSArray *imageModels;
@property (nonatomic,strong) NSMutableArray *arrayImageView;
@property (nonatomic,strong) UMComGridViewerController *viewerController;
@property (nonatomic,strong) UIViewController *fatherController;
@property (nonatomic,assign) float imageDeltaWidth;
@end
@implementation UMComGridView
/*
array 样例:
@[@[@"http://1",@"http://2"],@[],@[]]
*/
- (id)initWithArray:(NSArray *)array placeholder:(UIImage *)placeholder cellPad:(NSUInteger)cellPad
{
self = [super init];
if(self)
{
self.arrayImageView = [[NSMutableArray alloc] init];
self.cellPad = cellPad;
self.placeholder = placeholder;
for(int i = 0; i<9; i++)
{
UMComImageView *iv = [[UMComImageView alloc] initWithPlaceholderImage:placeholder];
[iv setIsAutoStart:NO];
iv.tag = i + 100;
[iv setCacheSecondes:A_WEEK_SECONDES];
iv.userInteractionEnabled = YES;
[self.arrayImageView addObject:iv];
}
[self setArray:array];
}
return self;
}
- (void)setImages:(NSArray *)images placeholder:(UIImage *)placeholder cellPad:(NSInteger)cellPad
{
if (self.arrayImageView == nil) {
self.arrayImageView = [[NSMutableArray alloc] init];
}else{
for (UMComImageView *imageView in self.arrayImageView) {
[imageView removeFromSuperview];
}
[self.arrayImageView removeAllObjects];
}
self.cellPad = cellPad;
self.placeholder = placeholder;
self.imageDeltaWidth = (self.frame.size.width-cellPad*2)/3;
for(int i = 0; i<9; i++)
{
UMComImageView *iv = [[[UMComImageView imageViewClassName] alloc] initWithPlaceholderImage:placeholder];
[iv setIsAutoStart:NO];
iv.tag = i + 100;
[iv setCacheSecondes:A_WEEK_SECONDES];
[iv setFrame:getImageViewRect(i,self.imageDeltaWidth, cellPad)];
iv.userInteractionEnabled = YES;
[self.arrayImageView addObject:iv];
}
[self setArray:images];
}
#pragma mark 根据size截取图片中间矩形区域的图片 这里的size是正方形
- (void)setArray:(NSArray *)array
{
self.imageModels = array;
if([array count])
{
CGSize size = getGridViewSize(array, self.cellPad, self.imageDeltaWidth);
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, size.height)];
int curImageCount = 0;
for (int i = 0;i < array.count;i++)
{
UMComImageUrl *imageModel = [array objectAtIndex:i];
if (curImageCount < self.arrayImageView.count) {
UMComImageView *iv = self.arrayImageView[curImageCount];
iv.needCutOff = YES;
if (iv.superview != self) {
[self addSubview:iv];
}
[iv setImageURL:imageModel.small_url_string placeHolderImage:self.placeholder];
curImageCount++;
//添加触控
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[iv addGestureRecognizer:tapGesture];
}
// if(imageModel.smallImageUrlString && imageModel.largeImageUrlString)
// {
//
//
// }
// else
// {
// UMLog(@"arr doesn't has 2 children![%@]",[imageModel description]);
// }
}
for (NSInteger i = array.count; i < 9; i++) {
UMComImageView *iv = self.arrayImageView[i];
[iv removeFromSuperview];
}
}
else
{
self.frame = CGRectZero;
}
}
- (void)removeAllSubviews
{
for(UMComImageView *iv in self.arrayImageView)
{
[iv removeFromSuperview];
}
}
- (void)tapImageView:(UITapGestureRecognizer *)tapGesture
{
NSInteger index = -1;
CGPoint point = [tapGesture locationInView:self];
for(UMComImageView *iv in self.arrayImageView)
{
if(CGRectContainsPoint(iv.frame, point))
{
index = iv.tag - 100;
break;
}
}
if(index == -1)
{
UMLog(@"sorry,you didn't select the imageview");
return;
}
self.viewerController = [[UMComGridViewerController alloc] initWithArray:self.imageModels index:index];
[self.viewerController setCacheSecondes:A_WEEK_SECONDES];
self.viewerController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
if (self.TapInImage) {
self.TapInImage(self.viewerController,self.arrayImageView[index]);
}
}
- (void)setPresentFatherViewController:(UIViewController *)viewController
{
self.fatherController = viewController;
}
- (void)setCacheSecondes:(NSTimeInterval)secondes
{
for(UMComImageView *iv in self.arrayImageView)
{
[iv setCacheSecondes:secondes];
}
}
- (void)startDownload
{
for(UMComImageView *iv in self.arrayImageView)
{
[iv startImageLoad];
}
}
@end
@@ -0,0 +1,33 @@
//
// UMComGridViewerController.h
// UMCommunity
//
// Created by luyiyuan on 14/9/2.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "UMImageProgressView.h"
@interface UMComGridViewerController : UIViewController<UIScrollViewDelegate>
- (id)initWithArray:(NSArray *)array index:(NSUInteger)index;
- (void)setArray:(NSArray *)array index:(NSUInteger)index;
//默认一周(60*60*24*7
- (void)setCacheSecondes:(NSTimeInterval)secondes;
@end
@interface UMZoomScrollView : UIScrollView<UIScrollViewDelegate,UMImageViewDelegate, UIActionSheetDelegate>
@property (nonatomic, strong) UMImageProgressView *imageView;
//默认一周(60*60*24*7
- (void)setCacheSecondes:(NSTimeInterval)secondes;
- (void)startDownload;
@end
@@ -0,0 +1,349 @@
//
// UMComGridViewerController.m
// UMCommunity
//
// Created by luyiyuan on 14/9/2.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import "UMComGridViewerController.h"
#import "UMImageProgressView.h"
#import "UMComShowToast.h"
#import <UMComDataStorage/UMComImageUrl.h>
typedef enum {
preImage = 0,
curImage = 1,
rearImage = 2,
}ImageLocation;
#define A_WEEK_SECONDES (60*60*24*7)
@interface UMComGridViewerController ()
@property (strong,nonatomic) UIPageControl *pageControl;
@property (strong,nonatomic) UIScrollView *scrollView;
@property (nonatomic,strong) NSArray *imageModels;
@property (nonatomic,strong) NSMutableArray *arrayImageView;
@property (nonatomic,strong) NSMutableArray *contentViews;
@property (nonatomic,assign) NSInteger totalPageCount;
@property (nonatomic,assign) NSInteger currentPageIndex;
@end
@implementation UMComGridViewerController
{
BOOL fisrtime;
}
- (id)initWithArray:(NSArray *)array index:(NSUInteger)index
{
self = [super init];
if(self)
{
self.arrayImageView = [[NSMutableArray alloc] init];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
[self.scrollView setDelegate:self];
for(int i=0;i<3; i++)
{
UMZoomScrollView *zoomScrollView = [[UMZoomScrollView alloc]initWithFrame:CGRectMake(i*self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
UMImageProgressView *iv = [[UMImageProgressView alloc] initWithFrame:CGRectMake(0, 0, zoomScrollView.frame.size.width, zoomScrollView.frame.size.height)];
iv.isAutoStart = NO;
[iv setTag:i];
[iv setCacheSecondes:A_WEEK_SECONDES];
zoomScrollView.imageView = iv;
[self.arrayImageView addObject:zoomScrollView];
}
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 20, self.view.bounds.size.width, 20)];
[self.view addSubview:self.scrollView];
[self.view addSubview:self.pageControl];
self.view.backgroundColor = [UIColor blackColor];
//添加触控
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[self.view addGestureRecognizer:tapGesture];
[self setArray:array index:index];
}
return self;
}
- (void)setArray:(NSArray *)array index:(NSUInteger)index
{
fisrtime = YES;
self.imageModels = array;
NSUInteger count = [array count];
if (count <= 9) {
[self.pageControl setNumberOfPages:count];
self.pageControl.currentPage = index;
}
self.currentPageIndex = index;
self.contentViews = [NSMutableArray arrayWithCapacity:3];
self.totalPageCount = count;
[self configContentView];
}
- (void)tapImageView:(UITapGestureRecognizer *)tapGesture
{
[self dismissViewControllerAnimated:YES completion:^{
for (UMZoomScrollView *zoomView in self.arrayImageView) {
zoomView.zoomScale = 1.0;
zoomView.imageView.center = CGPointMake(zoomView.frame.size.width/2, zoomView.frame.size.height/2);
}
}];
}
//默认一周(60*60*24*7
- (void)setCacheSecondes:(NSTimeInterval)secondes
{
for(UMZoomScrollView *iv in self.arrayImageView)
{
[iv setCacheSecondes:secondes];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.scrollView.delegate = self;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.scrollView.delegate = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x >= (2*CGRectGetWidth(scrollView.frame))) {
self.currentPageIndex = [self getValidNextPageIndexWithPageIndext:self.currentPageIndex+1];
[self configContentView];
}
if (scrollView.contentOffset.x <= 0) {
self.currentPageIndex = [self getValidNextPageIndexWithPageIndext:self.currentPageIndex-1];
[self configContentView];
}
self.pageControl.currentPage = self.currentPageIndex;
}
- (void)setTotalPageCount:(NSInteger)totalPageCount
{
_totalPageCount = totalPageCount;
if (_totalPageCount > 0) {
if (_totalPageCount == 1) {
self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
self.scrollView.contentOffset = CGPointMake(0, 0);
self.scrollView.scrollEnabled = NO;
}else{
self.scrollView.contentSize = CGSizeMake(3*CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
self.scrollView.scrollEnabled = YES;
}
}else{
[self.contentViews removeAllObjects];
}
}
- (void)configContentView
{
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self setScrollViewContentDataSource];
NSInteger counter = 0;
for (UMZoomScrollView *contenView in self.contentViews) {
contenView.frame = CGRectMake(self.scrollView.frame.size.width *counter+((self.scrollView.frame.size.width - contenView.frame.size.width)/2), 0, contenView.frame.size.width, contenView.frame.size.height);
counter ++;
[contenView startDownload];
[self.scrollView addSubview:contenView];
}
[self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0.0f) animated:NO];
}
- (UMZoomScrollView *)zoomScrollViewWithPageIndex:(NSInteger)pageIndex imageLocation:(ImageLocation)imageLocation
{
//
if (self.imageModels.count > 0) {
UMZoomScrollView *zoomView = nil;
if (imageLocation == preImage) {
zoomView = self.arrayImageView[0];
}else if (imageLocation == curImage){
zoomView = self.arrayImageView[1];
}else{
zoomView = self.arrayImageView[2];
}
if (zoomView.zoomScale > 1) {
[zoomView setZoomScale:1 animated:NO];
}
if (self.imageModels.count > pageIndex) {
;
UMComImageUrl *imageModel = self.imageModels[pageIndex];
zoomView.imageView.contentMode = UIViewContentModeScaleAspectFit;
[zoomView.imageView setImageURL:[NSURL URLWithString:imageModel.large_url_string]];
[zoomView.imageView setThumImageViewUrl:imageModel.small_url_string];
}
return zoomView;
}
return nil;
}
//获取将要加载图片
- (void)setScrollViewContentDataSource
{
[self.contentViews removeAllObjects];
@try {
NSInteger prePageIndex = [self getValidNextPageIndexWithPageIndext:self.currentPageIndex-1];
NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndext:self.currentPageIndex+1];
UMZoomScrollView *resaultView = [self zoomScrollViewWithPageIndex:prePageIndex imageLocation:preImage];
if(resaultView){
[self.contentViews addObject:resaultView];
}
resaultView = [self zoomScrollViewWithPageIndex:self.currentPageIndex imageLocation:curImage];
if(resaultView){
[self.contentViews addObject:resaultView];
}
resaultView = [self zoomScrollViewWithPageIndex:rearPageIndex imageLocation:rearImage];
if(resaultView){
[self.contentViews addObject:resaultView];
}
}
@catch (NSException *exception) {
NSLog(@"exception:%@", exception);
}
@finally {
}
}
- (NSInteger)getValidNextPageIndexWithPageIndext:(NSInteger)currentPageIndex;
{
if (currentPageIndex == -1) {
return self.totalPageCount - 1;
}else if (currentPageIndex == self.totalPageCount || currentPageIndex < -1){
return 0;
}else if (currentPageIndex > self.totalPageCount){
return self.totalPageCount - 1;
}else{
return currentPageIndex;
}
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
@end
@implementation UMZoomScrollView
{
UIActionSheet *actionSheet;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.backgroundColor = [UIColor clearColor];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(saveImageToAssest:)];
longPress.numberOfTouchesRequired = 1;
[self addGestureRecognizer:longPress];
}
return self;
}
- (void)setImageView:(UMImageProgressView *)imageView
{
_imageView = imageView;
_imageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
_imageView.backgroundColor = [UIColor clearColor];
if (imageView.isCacheImage) {
self.minimumZoomScale = 1.0;
self.maximumZoomScale = 5.0;
} else{
self.minimumZoomScale = 1.0;
self.maximumZoomScale = 1.0;
}
[self addSubview:_imageView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
(scrollView.bounds.size.width - scrollView.contentSize.width) /2 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
(scrollView.bounds.size.height - scrollView.contentSize.height) /2 : 0.0;
self.imageView.center = CGPointMake(scrollView.contentSize.width /2 + offsetX,scrollView.contentSize.height /2 + offsetY);
}
//默认一周(60*60*24*7
- (void)setCacheSecondes:(NSTimeInterval)secondes
{
[self.imageView setCacheSecondes:secondes];
}
- (void)startDownload
{
[self.imageView startImageLoad];
}
- (void)saveImageToAssest:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
if (![actionSheet isVisible]) {
actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:UMComLocalizedString(@"um_com_cancel", @"取消") destructiveButtonTitle:UMComLocalizedString(@"um_com_saveImageToAlbum", @"保存图片到相册")otherButtonTitles:nil, nil];
if (!actionSheet.superview) {
[actionSheet showInView:self.superview];
}
}
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
if (self.imageView.image) {
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
}
}
// 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
[UMComShowToast saveIamgeResultNotice:error];
}
@end
@@ -0,0 +1,15 @@
//
// UMComProgressView.h
// UMCommunity
//
// Created by luyiyuan on 14/9/3.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UMComProgressView : UIView
@property (assign, nonatomic) float progress;
- (id)initWithColor:(UIColor *)color;
@end
@@ -0,0 +1,96 @@
//
// UMComProgressView.m
// UMCommunity
//
// Created by luyiyuan on 14/9/3.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import "UMComProgressView.h"
@interface UMComProgressView ()
@property (nonatomic,strong) UIColor *schemeColor;
@property (nonatomic,strong) UIColor *progressColor;
@property (nonatomic,strong) UIColor *backgroundColor;
@property (nonatomic,strong) UIColor *textColor;
@end
@implementation UMComProgressView
- (id)initWithColor:(UIColor *)color
{
self = [self initWithFrame:CGRectMake(0, 0, 37, 37)];
if(self)
{
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.schemeColor = color;
CGColorRef progressCGColor = CGColorCreateCopyWithAlpha(color.CGColor, 1);
CGColorRef backgroundCGColor = CGColorCreateCopyWithAlpha(color.CGColor, 0.1);
CGColorRef textCGColor = CGColorCreateCopyWithAlpha(color.CGColor, 1);
_progressColor = [UIColor colorWithCGColor:progressCGColor];
_backgroundColor = [UIColor colorWithCGColor:backgroundCGColor];
_textColor = [UIColor colorWithCGColor:textCGColor];
CGColorRelease(progressCGColor);
CGColorRelease(backgroundCGColor);
CGColorRelease(textCGColor);
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.*/
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lineWidth = 5.f;
UIBezierPath *processBackgroundPath = [UIBezierPath bezierPath];
processBackgroundPath.lineWidth = lineWidth;
processBackgroundPath.lineCapStyle = kCGLineCapRound;
CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
CGFloat radius = (self.bounds.size.width - lineWidth)/2;
CGFloat startAngle = - ((float)M_PI / 2);
CGFloat endAngle = (2 * (float)M_PI) + startAngle;
[processBackgroundPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[self.backgroundColor set];
[processBackgroundPath stroke];
UIBezierPath *processPath = [UIBezierPath bezierPath];
processPath.lineCapStyle = kCGLineCapRound;
processPath.lineWidth = lineWidth;
endAngle = (self.progress * 2 * (float)M_PI) + startAngle;
[processPath addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[self.progressColor set];
[processPath stroke];
[self drawTextInContext:context];
}
- (void)drawTextInContext:(CGContextRef)context
{
UIFont *font = [UIFont systemFontOfSize:10];
CGRect allRect = self.bounds;
NSString *text = [NSString stringWithFormat:@"%i%%", (int)(_progress * 100.0f)];
CGSize textSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(30000, 13)];
float x = floorf(allRect.size.width / 2) + 3 + 0;
float y = floorf(allRect.size.height / 2) - 6 + 0;
CGContextSetFillColorWithColor(context, self.textColor.CGColor);
[text drawAtPoint:CGPointMake(x - textSize.width / 2.0, y) withFont:font];
}
@end
@@ -0,0 +1,18 @@
//
// UMImageViewWithProgress.h
// UMCommunity
//
// Created by luyiyuan on 14/9/3.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import "UMComImageView.h"
@interface UMImageProgressView : UMComImageView <UMComImageViewDelegate>
- (void)setThumImageViewUrl:(NSString *)urlString;
@property (nonatomic, strong) UMComImageView *thumImageView;
@end
@@ -0,0 +1,181 @@
//
// UMImageViewWithProgress.m
// UMCommunity
//
// Created by luyiyuan on 14/9/3.
// Copyright (c) 2014年 luyiyuan. All rights reserved.
//
#import "UMImageProgressView.h"
#import "UMComProgressView.h"
#import "UMComShowToast.h"
@interface UMImageProgressView ()
@property (nonatomic,strong) UMComProgressView *progressView;
@end
@implementation UMImageProgressView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.progressView = [[UMComProgressView alloc] initWithColor:[UIColor whiteColor]];
self.progressView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+40);
[self addSubview:self.progressView];
self.progressView.progress = 0.0f;
self.imageLoaderDelegate = self;
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void) startImageLoad
{
[super startImageLoad];
self.thumImageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-40);
self.progressView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+40);
if ([self ifNeedShowThumImage]) {
self.thumImageView.hidden = NO;
self.progressView.hidden = NO;
}else{
self.thumImageView.hidden = YES;
self.progressView.hidden = YES;
}
}
- (BOOL)ifNeedShowThumImage
{
if(!self.isCacheImage)
{
if (self.image) {
return NO;
}else{
return YES;
}
}else{
return NO;
}
}
- (void)cancelImageLoad
{
[super cancelImageLoad];
self.progressView.hidden = YES;
}
#pragma mark - UMComImageViewDelegate
- (void)umcomImageViewLoadedImageSizePercent:(float)percent imageView:(UMComImageView *)imageView
{
self.progressView.progress = percent;
[self.progressView setNeedsDisplay];
}
- (void)umcomImageViewLoadedImage:(UMComImageView *)imageView
{
self.progressView.hidden = YES;//zhangjunhua
self.thumImageView.hidden = YES;
UIScrollView * zoomView = (UIScrollView *)self.superview;
[self resetSizeWithURLImage:imageView];
if ([self.superview isKindOfClass:[UIScrollView class]]) {
zoomView.maximumZoomScale = 5;
}
//zhangjunhua---begin
zoomView.contentOffset = CGPointMake(0, 0);
zoomView.contentSize = CGSizeMake(0, 0);
CGRect org_rc = imageView.frame;
if (org_rc.size.height > zoomView.bounds.size.height) {
org_rc.origin.y = 0;
org_rc.origin.x = 0;
org_rc.size.height = imageView.image.size.height;
self.frame = org_rc;
zoomView.contentSize = imageView.bounds.size;
zoomView.contentOffset = CGPointMake(org_rc.origin.x, org_rc.origin.y);
}
//zhangjunhua---end
}
//- (void)umcomImageViewLoadedImage:(UMComImageView *)imageView
//{
// self.progressView.hidden = YES;
// self.thumImageView.hidden = YES;
// UIScrollView * zoomView = (UIScrollView *)self.superview;
// [self resetSizeWithURLImage:imageView];
// if ([self.superview isKindOfClass:[UIScrollView class]]) {
// zoomView.maximumZoomScale = 5;
// }
//}
- (void)umcomImageViewFailedToLoadImage:(UMComImageView *)imageView error:(NSError *)error
{
UIScrollView * zoomView = (UIScrollView *)self.superview;
if ([self.superview isKindOfClass:[UIScrollView class]]) {
zoomView.maximumZoomScale = 1;
}
self.progressView.hidden = YES;
[UMComShowToast showFetchResultTipWithError:error];
}
- (void)setThumImageViewUrl:(NSString *)urlString
{
if ([self ifNeedShowThumImage]) {
if (self.thumImageView == nil) {
UMComImageView *thumImageView = [[[UMComImageView imageViewClassName] alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
thumImageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-40);
thumImageView.hidden = YES;
thumImageView.needCutOff = YES;
[self addSubview:thumImageView];
self.thumImageView = thumImageView;
self.progressView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+40);
}else{
self.thumImageView.hidden = NO;
self.progressView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+40);
}
[self.thumImageView setImageURL:urlString placeHolderImage:self.thumImageView.placeholderImage];
}else{
self.thumImageView.hidden = YES;
}
}
- (void)resetSizeWithURLImage:(UMComImageView *)imageView
{
self.thumImageView.hidden = YES;
UIScrollView * zoomView = (UIScrollView *)self.superview;
if ([self.superview isKindOfClass:[UIScrollView class]] && zoomView.zoomScale > 1) {
// zoomView.zoomScale = 1;
}else{
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGSize viewSize = [UIScreen mainScreen].bounds.size;
CGSize imageSize = imageView.image.size;
int height = viewSize.width * imageSize.height / imageSize.width;
// imageView.frame = CGRectMake(0,viewSize.height/2-height/2, viewSize.width, height);
imageView.frame = CGRectMake(0, 0, zoomView.bounds.size.width, zoomView.bounds.size.height);
}
// NSLog(@"imageView center is :%@",NSStringFromCGPoint(imageView.center));
}
@end