MJPhotoToolbar.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // MJPhotoToolbar.m
  3. // FingerNews
  4. //
  5. // Created by mj on 13-9-24.
  6. // Copyright (c) 2013年 itcast. All rights reserved.
  7. //
  8. #import "MJPhotoToolbar.h"
  9. #import "MJPhoto.h"
  10. #import "AppDelegate.h"
  11. @interface MJPhotoToolbar()
  12. {
  13. AppDelegate * app;
  14. // 显示页码
  15. UILabel *_indexLabel;
  16. UIButton *_saveImageBtn;
  17. }
  18. @end
  19. @implementation MJPhotoToolbar
  20. @synthesize Delegate;
  21. @synthesize DeleteImage;
  22. - (id)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // Initialization code
  27. app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  28. }
  29. return self;
  30. }
  31. - (void)setPhotos:(NSArray *)photos
  32. {
  33. _photos = photos;
  34. if (_photos.count > 1) {
  35. _indexLabel = [[UILabel alloc] init];
  36. _indexLabel.font = [UIFont boldSystemFontOfSize:20];
  37. _indexLabel.frame = self.bounds;
  38. _indexLabel.backgroundColor = [UIColor clearColor];
  39. _indexLabel.textColor = [UIColor whiteColor];
  40. _indexLabel.textAlignment = NSTextAlignmentCenter;
  41. _indexLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  42. [self addSubview:_indexLabel];
  43. }
  44. // 保存图片按钮
  45. CGFloat btnWidth = self.bounds.size.height;
  46. _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  47. [_saveImageBtn setHidden:YES];
  48. _saveImageBtn.frame = CGRectMake(20, 0, btnWidth, btnWidth);
  49. _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  50. [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon.png"] forState:UIControlStateNormal];
  51. [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon_highlighted.png"] forState:UIControlStateHighlighted];
  52. [_saveImageBtn setImage:[UIImage imageNamed:@"photo-gallery-trashcan.png"] forState:UIControlStateHighlighted];
  53. [_saveImageBtn addTarget:self action:@selector(downLoadImg) forControlEvents:UIControlEventTouchUpInside];
  54. [self addSubview:_saveImageBtn];
  55. }
  56. - (void)deleteThisImage
  57. {
  58. if ( [Delegate respondsToSelector:@selector(DeleteThisImage:)] ) {
  59. [Delegate DeleteThisImage:_currentPhotoIndex];
  60. }
  61. }
  62. - (void)downLoadImg {
  63. if ( [Delegate respondsToSelector:@selector(DeleteThisImage:)] ) {
  64. [Delegate downLoadThisImage:_currentPhotoIndex];
  65. }
  66. }
  67. - (void)saveImage
  68. {
  69. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  70. MJPhoto *photo = _photos[_currentPhotoIndex];
  71. UIImageWriteToSavedPhotosAlbum(photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  72. });
  73. }
  74. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  75. {
  76. if (error) {
  77. [MBProgressHUD showSuccess:YZMsg(@"保存失败") toView:nil];
  78. } else {
  79. MJPhoto *photo = _photos[_currentPhotoIndex];
  80. photo.save = YES;
  81. _saveImageBtn.enabled = NO;
  82. [MBProgressHUD showSuccess:YZMsg(@"保存成功") toView:nil];
  83. }
  84. }
  85. - (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
  86. {
  87. _currentPhotoIndex = currentPhotoIndex;
  88. // 更新页码
  89. _indexLabel.text = [NSString stringWithFormat:@"%zd / %zd", _currentPhotoIndex + 1, _photos.count];
  90. // MJPhoto *photo = _photos[_currentPhotoIndex];
  91. // 按钮
  92. // _saveImageBtn.enabled = photo.image != nil && !photo.save;
  93. }
  94. @end