MJPhotoToolbar.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "MBProgressHUD+Add.h"
  11. @interface MJPhotoToolbar()
  12. {
  13. // 显示页码
  14. UILabel *_indexLabel;
  15. UIButton *_saveImageBtn;
  16. }
  17. @end
  18. @implementation MJPhotoToolbar
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame:frame];
  22. if (self) {
  23. // Initialization code
  24. }
  25. return self;
  26. }
  27. - (void)setPhotos:(NSArray *)photos
  28. {
  29. _photos = photos;
  30. if (_photos.count > 1) {
  31. _indexLabel = [[UILabel alloc] init];
  32. _indexLabel.font = [UIFont boldSystemFontOfSize:20];
  33. _indexLabel.frame = self.bounds;
  34. _indexLabel.backgroundColor = [UIColor clearColor];
  35. _indexLabel.textColor = [UIColor whiteColor];
  36. _indexLabel.textAlignment = NSTextAlignmentCenter;
  37. _indexLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  38. [self addSubview:_indexLabel];
  39. }
  40. // 保存图片按钮
  41. // CGFloat btnWidth = self.bounds.size.height;
  42. // _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  43. // _saveImageBtn.frame = CGRectMake(20, 0, btnWidth, btnWidth);
  44. // _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  45. // [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon.png"] forState:UIControlStateNormal];
  46. // [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon_highlighted.png"] forState:UIControlStateHighlighted];
  47. // [_saveImageBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
  48. // [self addSubview:_saveImageBtn];
  49. }
  50. - (void)saveImage
  51. {
  52. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  53. MJPhoto *photo = _photos[_currentPhotoIndex];
  54. UIImageWriteToSavedPhotosAlbum(photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  55. });
  56. }
  57. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  58. {
  59. if (error) {
  60. // [MBProgressHUD showSuccess:@"保存失败" toView:nil];
  61. } else {
  62. MJPhoto *photo = _photos[_currentPhotoIndex];
  63. photo.save = YES;
  64. _saveImageBtn.enabled = NO;
  65. // [MBProgressHUD showSuccess:@"成功保存到相册" toView:nil];
  66. }
  67. }
  68. - (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
  69. {
  70. _currentPhotoIndex = currentPhotoIndex;
  71. // 更新页码
  72. _indexLabel.text = [NSString stringWithFormat:@"%d / %d", _currentPhotoIndex + 1, _photos.count];
  73. MJPhoto *photo = _photos[_currentPhotoIndex];
  74. // 按钮
  75. _saveImageBtn.enabled = photo.image != nil && !photo.save;
  76. }
  77. @end