HBShowImageControl.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // HBShowImageControl.m
  3. // MyTest
  4. //
  5. // Created by weqia on 13-8-8.
  6. // Copyright (c) 2013年 weqia. All rights reserved.
  7. //
  8. #import "HBShowImageControl.h"
  9. #import "NSStrUtil.h"
  10. #import "UIImageView+HBHttpCache.h"
  11. #import "ObjUrlData.h"
  12. #import "JSONKit.h"
  13. @implementation HBShowImageControl
  14. @synthesize delegate,bFirstSmall,smallTag,bigTag,controller,larges;
  15. #pragma -mark 覆盖父类的方法
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. // Initialization code
  21. }
  22. return self;
  23. }
  24. #pragma -mark 私有方法
  25. -(void)layoutImages
  26. {
  27. int count=(int)[_imgurls count];
  28. if(count==1) {
  29. if (bFirstSmall) {
  30. [self drawLessThree];
  31. } else {
  32. [self drawSingleImage:[_imgurls objectAtIndex:0]];
  33. }
  34. }
  35. else if(count<=3)
  36. [self drawLessThree];
  37. else if(count==4)
  38. [self drawFour];
  39. else
  40. [self drawMoreFour];
  41. [self drawFile];
  42. }
  43. -(void)drawFile
  44. {
  45. if([_files count]>0){
  46. float y;
  47. int imgCount=(int)[_imgurls count];
  48. if(imgCount==0){
  49. y=0;
  50. }else if(imgCount==1){
  51. y=MAX_HEIGHT;
  52. }else{
  53. y=([_imgurls count]/4+1)*(IMAGE_SPACE+IMAGE_SIZE);
  54. }
  55. UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, y+5, 44, 29)];
  56. imageView.image=[UIImage imageNamed:@"f_attach"];
  57. [self addSubview:imageView];
  58. UIImageView * countView=[[UIImageView alloc]initWithFrame:CGRectMake(22, 10, 18, 18)];
  59. countView.image=[UIImage imageNamed:@"f_attach_count"];
  60. UILabel * countLabel=[[UILabel alloc]initWithFrame:CGRectMake(22, 10, 18, 18)];
  61. countLabel.backgroundColor=[UIColor clearColor];
  62. countLabel.textAlignment=NSTextAlignmentCenter;
  63. countLabel.textColor=[UIColor whiteColor];
  64. countLabel.font=[UIFont systemFontOfSize:12];
  65. countLabel.text=[NSString stringWithFormat:@"%ld",[_files count]];
  66. [imageView addSubview:countView];
  67. [imageView addSubview:countLabel];
  68. UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookFileAction:)];
  69. [imageView addGestureRecognizer:tap];
  70. imageView.userInteractionEnabled=YES;
  71. }
  72. }
  73. -(void)uploadFinish
  74. {
  75. if(delegate&&[delegate respondsToSelector:@selector(showImageControlFinishLoad:)])
  76. [delegate showImageControlFinishLoad:self];
  77. }
  78. -(void)drawSingleImage:(ObjUrlData*)url
  79. {
  80. UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT)];
  81. [self addSubview:imageView];
  82. [self drawImage:imageView file:url];
  83. [self uploadFinish];
  84. return;
  85. // NSMutableString * smlStr=[[NSMutableString alloc]init];
  86. // NSMutableString * bigStr=[[NSMutableString alloc]init];
  87. // if([NSStrUtil notEmptyOrNull:url.url]){
  88. //// [smlStr appendFormat:@"%@&th=%d",url.url,smallTag];
  89. //// [bigStr appendFormat:@"%@&th=%d",url.url,bigTag];
  90. // [smlStr appendFormat:@"%@",url.url];
  91. // [bigStr appendFormat:@"%@",url.url];
  92. // }
  93. /*
  94. [_bigUrls addObject:url.url];
  95. UIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MAX_WIDTH, MAX_HEIGHT)];
  96. imageView.image=[UIImage imageNamed:@"img_loading_for_talk"];
  97. [self addSubview:imageView];
  98. [ _imageViews addObject:imageView];
  99. UIActivityIndicatorView * indicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(90, 50, 20, 20)];
  100. [imageView addSubview:indicator];
  101. [indicator startAnimating];
  102. void(^block)(UIImage * image)=^(UIImage * image){
  103. if(image==nil)
  104. return ;
  105. [indicator stopAnimating];
  106. [indicator removeFromSuperview];
  107. CGSize size=image.size;
  108. float scale=size.height/size.width;
  109. float width=MAX_HEIGHT/scale,height=MAX_HEIGHT;
  110. if(scale<=(MAX_HEIGHT/MAX_WIDTH)&&width>=MAX_WIDTH)
  111. {
  112. width=MAX_WIDTH;
  113. height=width*scale;
  114. }
  115. scale= width/size.width;
  116. if(scale!=1){
  117. image=[UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
  118. }
  119. size=image.size;
  120. imageView.frame=CGRectMake(0, 0, width, height);
  121. imageView.image=image;
  122. UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookImageAction:)];
  123. imageView.userInteractionEnabled=YES;
  124. [imageView addGestureRecognizer:tap];
  125. };
  126. [[HBHttpImageDownloader shareDownlader] downBitmapWithURL:url.url process:nil complete:^(UIImage * image, NSData * data, NSError * error, BOOL finish) {
  127. block(image);
  128. [self uploadFinish];
  129. } option:HBHttpImageDownloaderOptionUseCache valueReturn:nil];
  130. */
  131. }
  132. -(void)drawLessThree
  133. {
  134. int count=(int)[_imgurls count];
  135. for(int i=0;i<count;i++)
  136. {
  137. UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake((IMAGE_SIZE+IMAGE_SPACE)*i, 0, IMAGE_SIZE, IMAGE_SIZE)];
  138. ObjUrlData * file=[_imgurls objectAtIndex:i];
  139. [self addSubview:imageView];
  140. [self drawImage:imageView file:file];
  141. if(count-1==i){
  142. [self uploadFinish];
  143. }
  144. }
  145. }
  146. -(void)drawFour
  147. {
  148. int count=(int)[_imgurls count];
  149. for(int i=0;i<count;i++)
  150. {
  151. UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake((IMAGE_SPACE+IMAGE_SIZE)*(i%2),(IMAGE_SPACE+IMAGE_SIZE)*(i/2), IMAGE_SIZE, IMAGE_SIZE)];
  152. ObjUrlData * file=[_imgurls objectAtIndex:i];
  153. [self addSubview:imageView];
  154. [self drawImage:imageView file:file];
  155. if(i==count-1)
  156. [self uploadFinish];
  157. }
  158. }
  159. -(void)drawMoreFour
  160. {
  161. int count=(int)[_imgurls count];
  162. for(int i=0;i<count;i++)
  163. {
  164. UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake((IMAGE_SPACE+IMAGE_SIZE)*(i%3),(IMAGE_SPACE+IMAGE_SIZE)*(i/3), IMAGE_SIZE, IMAGE_SIZE)];
  165. ObjUrlData * file=[_imgurls objectAtIndex:i];
  166. [self addSubview:imageView];
  167. [self drawImage:imageView file:file];
  168. if(i==count-1)
  169. [self uploadFinish];
  170. }
  171. }
  172. -(void)drawImage:(UIImageView*)imageView file:(ObjUrlData*)file
  173. {
  174. // NSMutableString * smlStr=[[NSMutableString alloc]init];
  175. // NSMutableString * bigStr=[[NSMutableString alloc]init];
  176. // if([NSStrUtil notEmptyOrNull:file.url]){
  177. // [smlStr appendFormat:@"%@&th=%d",file.url,smallTag];
  178. // [bigStr appendFormat:@"%@&th=%d",file.url,bigTag];
  179. // }
  180. [_bigUrls addObject:file.url];
  181. [ _imageViews addObject:imageView];
  182. UIActivityIndicatorView * indicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake((imageView.frame.size.width-20)/2, (imageView.frame.size.height-20)/2, 20, 20)];
  183. [imageView addSubview:indicator];
  184. [indicator startAnimating];
  185. imageView.contentMode=UIViewContentModeScaleAspectFill;
  186. imageView.clipsToBounds=YES;
  187. // __block UIImageView * wimageView=imageView;
  188. [imageView sd_setImageWithURL:[NSURL URLWithString:file.url] placeholderImage:[UIImage imageNamed:@"Default_Gray"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  189. [indicator stopAnimating];
  190. [indicator removeFromSuperview];
  191. UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookImageAction:)];
  192. imageView.userInteractionEnabled=YES;
  193. [imageView addGestureRecognizer:tap];
  194. imageView.image = image;
  195. }];
  196. // [imageView setImageWithURL:file.url layout:UIImageViewLayoutNone placeholderImage:[UIImage imageNamed:@"Default_Gray"] process:nil complete:^(UIImage * image, NSData * data, NSError *error, BOOL finish) {
  197. // [indicator stopAnimating];
  198. // [indicator removeFromSuperview];
  199. // UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookImageAction:)];
  200. // wimageView.userInteractionEnabled=YES;
  201. // [wimageView addGestureRecognizer:tap];
  202. // wimageView.image = image;
  203. //
  204. //
  205. // } option:HBHttpImageDownloaderOptionUseCache];
  206. }
  207. #pragma -mark 事件响应方法
  208. -(void)lookFileAction:(UIGestureRecognizer*)sender
  209. {
  210. if(delegate&&[delegate respondsToSelector:@selector(lookFileAction:files:)]){
  211. [delegate lookFileAction:self files:_files];
  212. }
  213. }
  214. -(void)lookImageAction:(UIGestureRecognizer*)sender
  215. {
  216. if(delegate&&[delegate respondsToSelector:@selector(lookImageAction:)])
  217. [delegate lookImageAction:self];
  218. _imageList=[[HBImageViewList alloc]initWithFrame:[UIScreen mainScreen].bounds];
  219. [_imageList addTarget:self tapOnceAction:@selector(dismissImageAction:)];
  220. int index=(int)[_imageViews indexOfObject:sender.view];
  221. // if(IOS_VERSION<7.0){
  222. // [UIApplication sharedApplication].statusBarHidden=YES;
  223. // }
  224. UIImageView * view=(UIImageView*)sender.view;
  225. // NSString * url=[_bigUrls objectAtIndex:index];
  226. ObjUrlData* p = [larges objectAtIndex:index];
  227. NSString * url=p.url;
  228. p = nil;
  229. _util=[[NSImageUtil alloc]init];
  230. int count=(int)[_imageViews count];
  231. for(int i=0;i<count;i++){
  232. UIImage * image=((UIImageView*)[_imageViews objectAtIndex:i]).image;
  233. if(image){
  234. [_images addObject:image];
  235. }
  236. // else
  237. // {
  238. // [_images addObject:[UIImage imageNamed:@"img_loading_for_talk"] ];
  239. // }
  240. }
  241. [_util showBigImageWithUrl:url fromView:view complete:^(UIView * backView) {
  242. [backView setHidden:YES];
  243. // [_imageList addImagesURL:_bigUrls withSmallImage:_images];
  244. [_imageList addImagesURL:larges withSmallImage:_images];
  245. [_imageList setIndex:index];
  246. [self.window addSubview:_imageList];
  247. [UIApplication sharedApplication].statusBarHidden = YES;
  248. }];
  249. }
  250. -(void)dismissImageAction:(UIImageView*)sender
  251. {
  252. [UIApplication sharedApplication].statusBarHidden = NO;
  253. int index=(int)[_imageList.imageViews indexOfObject:sender.superview];
  254. if (index < 0) {
  255. return;
  256. }
  257. UIImageView * view=[_imageViews objectAtIndex:index];
  258. [_imageList removeFromSuperview];
  259. NSString * url=[_bigUrls objectAtIndex:index];
  260. [_util goBackToView:view withImageUrl:url];
  261. // if(IOS_VERSION<7.0){
  262. // [UIApplication sharedApplication].statusBarHidden=NO;
  263. // }
  264. }
  265. #pragma -mark 接口方法
  266. -(void)setImagesFileStr:(NSString*)fileStr
  267. {
  268. if(fileStr==nil)
  269. return;
  270. if ([fileStr isKindOfClass:[NSArray class]]) {
  271. if(((NSArray*)fileStr).count==0)
  272. return;
  273. id object=[((NSArray*)fileStr) objectAtIndex:0];
  274. if([object isKindOfClass:[ObjUrlData class]]){
  275. [self setImagesWithFiles:(NSArray*)fileStr];
  276. return;
  277. }
  278. fileStr = [fileStr JSONString];
  279. }
  280. NSArray *objArr = [ObjUrlData getDataArray:fileStr];
  281. [self setImagesWithFiles:objArr];
  282. }
  283. -(void)setImagesWithFiles:(NSArray*)files1
  284. {
  285. NSMutableArray * images=[[NSMutableArray alloc]init];
  286. NSMutableArray * files=[[NSMutableArray alloc]init];
  287. int count=(int)[files1 count];
  288. for(int i=0;i<count;i++){
  289. ObjUrlData * data =[files1 objectAtIndex:i];
  290. if ([data.url isKindOfClass:[NSString class]]) {
  291. if([data.url length]>0){
  292. NSArray * array=[data.mime componentsSeparatedByString:@"/"];
  293. NSString * mime=[array objectAtIndex:0];
  294. if([mime isEqualToString:@"image"])
  295. [images addObject:data];
  296. else{
  297. [files addObject:data];
  298. }
  299. }
  300. }
  301. }
  302. //weibo说说预展示图片的Url(不清晰)
  303. _imgurls=images;
  304. _files=files;
  305. _bigUrls=[[NSMutableArray alloc]init];
  306. _images=[[NSMutableArray alloc]init];
  307. _imageViews=[[NSMutableArray alloc]init];
  308. for(UIView * view in self.subviews)
  309. [view removeFromSuperview];
  310. [self layoutImages];
  311. }
  312. +(float)heightForFileStr:(NSString*)fileStr
  313. {
  314. if(fileStr==nil)
  315. return 0;
  316. if ([fileStr isKindOfClass:[NSArray class]]) {
  317. if(((NSArray*)fileStr).count==0)
  318. return 0;
  319. id object=[((NSArray*)fileStr) objectAtIndex:0];
  320. if([object isKindOfClass:[ObjUrlData class]]){
  321. return [self heightForFiles:(NSArray*)fileStr];
  322. }
  323. fileStr = [fileStr JSONString];
  324. }
  325. NSArray *objArr = [ObjUrlData getDataArray:fileStr];
  326. return [self heightForFiles:objArr];
  327. }
  328. +(float)heightForFiles:(NSArray*)files1
  329. {
  330. int imageCount=0;
  331. int count=(int)[files1 count];
  332. for(int i=0;i<count;i++){
  333. ObjUrlData * data =[files1 objectAtIndex:i];
  334. NSArray * array=[data.mime componentsSeparatedByString:@"/"];
  335. NSString * mime=[array objectAtIndex:0];
  336. if([mime isEqualToString:@"image"])
  337. imageCount++;
  338. }
  339. float offset;
  340. if(imageCount==count){
  341. offset=0;
  342. }else{
  343. offset=40;
  344. }
  345. if(imageCount==0)
  346. return offset;
  347. else if(imageCount==1){
  348. return MAX_HEIGHT+offset;
  349. }else{
  350. if (imageCount % 3 == 0) {
  351. return (imageCount/3)*(IMAGE_SIZE+IMAGE_SPACE)+offset;
  352. }else {
  353. return (imageCount/3+1)*(IMAGE_SIZE+IMAGE_SPACE)+offset;
  354. }
  355. }
  356. }
  357. @end