MJPhotoToolbar.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #import "SVProgressHUD.h"
  12. @interface MJPhotoToolbar()
  13. {
  14. // 显示页码
  15. UILabel *_indexLabel;
  16. UIButton *_saveImageBtn;
  17. }
  18. @end
  19. @implementation MJPhotoToolbar
  20. - (id)initWithFrame:(CGRect)frame
  21. {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  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. [SVProgressHUD showSuccessWithStatus:ASLocalizedString(@"保存失败")];
  61. } else {
  62. MJPhoto *photo = _photos[_currentPhotoIndex];
  63. photo.save = YES;
  64. _saveImageBtn.enabled = NO;
  65. [SVProgressHUD showSuccessWithStatus:ASLocalizedString(@"成功保存到相册")];
  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