123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // MJPhotoToolbar.m
- // FingerNews
- //
- // Created by mj on 13-9-24.
- // Copyright (c) 2013年 itcast. All rights reserved.
- //
- #import "MJPhotoToolbar.h"
- #import "MJPhoto.h"
- #import "MBProgressHUD+Add.h"
- @interface MJPhotoToolbar()
- {
- // 显示页码
- UILabel *_indexLabel;
- UIButton *_saveImageBtn;
- }
- @end
- @implementation MJPhotoToolbar
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (void)setPhotos:(NSArray *)photos
- {
- _photos = photos;
-
- if (_photos.count > 1) {
- _indexLabel = [[UILabel alloc] init];
- _indexLabel.font = [UIFont boldSystemFontOfSize:20];
- _indexLabel.frame = self.bounds;
- _indexLabel.backgroundColor = [UIColor clearColor];
- _indexLabel.textColor = [UIColor whiteColor];
- _indexLabel.textAlignment = NSTextAlignmentCenter;
- _indexLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
- [self addSubview:_indexLabel];
- }
-
- // 保存图片按钮
- CGFloat btnWidth = self.bounds.size.height;
- _saveImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _saveImageBtn.frame = CGRectMake(20, 0, btnWidth + 20, btnWidth);
- _saveImageBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight;
-
- [_saveImageBtn setTitle:@"保存" forState:UIControlStateNormal];
- [_saveImageBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
- [_saveImageBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
-
- //danson将图片保存到相册
- [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon.png"] forState:UIControlStateNormal];
- [_saveImageBtn setImage:[UIImage imageNamed:@"MJPhotoBrowser.bundle/save_icon_highlighted.png"] forState:UIControlStateHighlighted];
-
- [_saveImageBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_saveImageBtn];
- }
- - (void)saveImage
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- MJPhoto *photo = _photos[_currentPhotoIndex];
- UIImageWriteToSavedPhotosAlbum(photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
- });
- }
- - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
- {
- if (error) {
- [MBProgressHUD showSuccess:@"保存失败" toView:nil];
- } else {
- MJPhoto *photo = _photos[_currentPhotoIndex];
- photo.save = YES;
- _saveImageBtn.enabled = NO;
- [MBProgressHUD showSuccess:@"成功保存到相册" toView:nil];
- }
- }
- - (void)setCurrentPhotoIndex:(NSUInteger)currentPhotoIndex
- {
- _currentPhotoIndex = currentPhotoIndex;
-
- // 更新页码
- _indexLabel.text = [NSString stringWithFormat:@"%d / %d", (int)_currentPhotoIndex + 1, (int)_photos.count];
-
- MJPhoto *photo = _photos[_currentPhotoIndex];
- // 按钮
- _saveImageBtn.enabled = photo.image != nil && !photo.save;
-
- //当_currentPhotoIndex为0时候 photo.image总是==nil 为毛呢 必须有初始的偏移量才行
- // NSLog(@"%@编辑",_saveImageBtn.enabled?@"可以":@"不可");
- // NSLog(@"%@编辑",!photo.save?@"可以":@"不可");
- // NSLog(@"%@编辑",photo.image != nil?@"可以":@"不可");
- }
- @end
|