MJPhotoToolbar.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 + 20, btnWidth);
  44. _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  45. [_saveImageBtn setTitle:@"保存" forState:UIControlStateNormal];
  46. [_saveImageBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  47. [_saveImageBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  48. //danson将图片保存到相册
  49. [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon.png"] forState:UIControlStateNormal];
  50. [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon_highlighted.png"] forState:UIControlStateHighlighted];
  51. [_saveImageBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
  52. [self addSubview:_saveImageBtn];
  53. }
  54. - (void)saveImage
  55. {
  56. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  57. MJPhoto *photo = _photos[_currentPhotoIndex];
  58. UIImageWriteToSavedPhotosAlbum(photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  59. });
  60. }
  61. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  62. {
  63. if (error) {
  64. [MBProgressHUD showSuccess:@"保存失败" toView:nil];
  65. } else {
  66. MJPhoto *photo = _photos[_currentPhotoIndex];
  67. photo.save = YES;
  68. _saveImageBtn.enabled = NO;
  69. [MBProgressHUD showSuccess:@"成功保存到相册" toView:nil];
  70. }
  71. }
  72. - (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
  73. {
  74. _currentPhotoIndex = currentPhotoIndex;
  75. // 更新页码
  76. _indexLabel.text = [NSString stringWithFormat:@"%d / %d", (int)_currentPhotoIndex + 1, (int)_photos.count];
  77. MJPhoto *photo = _photos[_currentPhotoIndex];
  78. // 按钮
  79. _saveImageBtn.enabled = photo.image != nil && !photo.save;
  80. //当_currentPhotoIndex为0时候 photo.image总是==nil 为毛呢 必须有初始的偏移量才行
  81. // NSLog(@"%@编辑",_saveImageBtn.enabled?@"可以":@"不可");
  82. // NSLog(@"%@编辑",!photo.save?@"可以":@"不可");
  83. // NSLog(@"%@编辑",photo.image != nil?@"可以":@"不可");
  84. }
  85. @end