AQTool.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // AQTool.m
  3. // Picture
  4. //
  5. // Created by aiqing on 16/1/10.
  6. // Copyright © 2016年 aiqing. All rights reserved.
  7. //
  8. #import "AQTool.h"
  9. #import "AppDelegate.h"
  10. static CGRect oldframe;
  11. UIWindow *window;
  12. @implementation AQTool
  13. +(void)showImage:(UIImageView *)avatarImageView{
  14. UIImage *image=avatarImageView.image;
  15. window=[UIApplication sharedApplication].keyWindow;
  16. UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  17. oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:[AppDelegate shareDelegate].window];
  18. backgroundView.backgroundColor=[UIColor blackColor];
  19. backgroundView.alpha=0;
  20. UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
  21. imageView.image=image;
  22. imageView.userInteractionEnabled=YES;
  23. imageView.tag=avatarImageView.tag;
  24. [backgroundView addSubview:imageView];
  25. // 保存图片按钮
  26. UIButton * _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  27. _saveImageBtn.frame = CGRectMake(20, [UIScreen mainScreen].bounds.size.height - 100,40, 40);
  28. _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  29. [_saveImageBtn setImage:[UIImage imageNamed:@"save_icon.png"] forState:UIControlStateNormal];
  30. [_saveImageBtn setImage:[UIImage imageNamed:@"save_icon_highlighted.png"] forState:UIControlStateHighlighted];
  31. _saveImageBtn.tag = avatarImageView.tag;
  32. NSLog( @"buttonbuttonbutton%zi",_saveImageBtn.tag);
  33. _saveImageBtn.backgroundColor = [UIColor clearColor];
  34. [_saveImageBtn addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
  35. [backgroundView addSubview:_saveImageBtn];
  36. [[AppDelegate shareDelegate].window addSubview:backgroundView];
  37. UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
  38. [backgroundView addGestureRecognizer: tap];
  39. [UIView animateWithDuration:0.3 animations:^{
  40. imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
  41. backgroundView.alpha=1;
  42. } completion:^(BOOL finished) {
  43. }];
  44. }
  45. +(void)hideImage:(UITapGestureRecognizer*)tap{
  46. UIView *backgroundView=tap.view;
  47. UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
  48. [UIView animateWithDuration:0.3 animations:^{
  49. imageView.frame=oldframe;
  50. backgroundView.alpha=0;
  51. } completion:^(BOOL finished) {
  52. [backgroundView removeFromSuperview];
  53. }];
  54. }
  55. +(void)saveImage:(id)imageBotton
  56. {
  57. UIImageView * imageView = [UIImageView new];
  58. UIButton * button = (UIButton *)imageBotton;
  59. imageView.tag = button.tag;
  60. imageView = (UIImageView*)[button.superview viewWithTag:button.tag];
  61. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  62. UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  63. });
  64. }
  65. + (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  66. {
  67. if (error) {
  68. [[AppDelegate shareDelegate].window makeToast:@"保存失败" duration:1.5 position:CSToastPositionCenter];
  69. //NSLog(@"保存失败,权限未开?");
  70. } else {
  71. [[AppDelegate shareDelegate].window makeToast:@"成功保存到相册" duration:1.5 position:CSToastPositionCenter];
  72. //NSLog(@"成功保存到相册,可去照片查询");
  73. }
  74. }
  75. @end