| | |
| | | #import "MJRefreshNormalHeader.h" |
| | | #import "MJRefreshAutoNormalFooter.h" |
| | | #import "HDLEZDevMsgListModel.h" |
| | | @interface HDLEZDeviceMsgListViewController () |
| | | #import "MWPhotoBrowser.h" |
| | | #import "SDWebImageManager.h" |
| | | #import <Photos/Photos.h> |
| | | @interface HDLEZDeviceMsgListViewController ()<MWPhotoBrowserDelegate> |
| | | @property (nonatomic, strong) HDLEZDeviceMsgListView *msgListV;//消息列表view |
| | | |
| | | @property (nonatomic, strong) MJRefreshNormalHeader *header;//刷新header |
| | |
| | | |
| | | @property (nonatomic, assign) NSInteger pageSize;//分页容量 |
| | | |
| | | @property (nonatomic, strong) NSMutableArray <MWPhoto *>*photos; // 预览图片信息 |
| | | @property (nonatomic, strong) NSString *currentPreviewPhoto; // 当前预览的图片 |
| | | |
| | | @end |
| | | |
| | | @implementation HDLEZDeviceMsgListViewController |
| | |
| | | make.top.equalTo(self.view).offset(HDLEZ_APP_TOP_BAR_HEIGHT); |
| | | make.left.bottom.right.equalTo(self.view); |
| | | }]; |
| | | // _msgListV.choseDelegate=self; |
| | | __weak __typeof(self)weakSelf = self; |
| | | [_msgListV previewPicture:^(NSString * _Nonnull pictureUrl) { |
| | | [weakSelf previewPictureWithUrl:pictureUrl]; |
| | | }]; |
| | | |
| | | //下拉刷新 |
| | | // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法) |
| | |
| | | weakSelf.msgListModel=[HDLEZDevMsgListModel yy_modelWithDictionary:responseData.data]; |
| | | if (responseData.success) { |
| | | weakSelf.msgListModel=[HDLEZDevMsgListModel yy_modelWithDictionary:responseData.data]; |
| | | if (weakSelf.msgListModel.list.count!=0) { |
| | | [weakSelf.msgList addObjectsFromArray:weakSelf.msgListModel.list]; |
| | | weakSelf.msgListV.msgList=weakSelf.msgList; |
| | | if (weakSelf.currentPageNo == 1) { |
| | | [weakSelf.msgList removeAllObjects]; |
| | | } |
| | | [weakSelf.msgList addObjectsFromArray:weakSelf.msgListModel.list]; |
| | | weakSelf.msgListV.msgList=weakSelf.msgList; |
| | | if (weakSelf.msgListModel.list.count == weakSelf.pageSize) { |
| | | weakSelf.currentPageNo+=1; |
| | | }else{ |
| | | } else { |
| | | [weakSelf.footer endRefreshingWithNoMoreData]; |
| | | } |
| | | |
| | | }else{ |
| | | [weakSelf.view makeToast:responseData.message |
| | | duration:1.5 |
| | |
| | | }]; |
| | | } |
| | | |
| | | #pragma mark MWPhotoBrowserDelegate |
| | | - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser { |
| | | return self.photos.count; |
| | | } |
| | | |
| | | - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index { |
| | | if (index < self.photos.count) { |
| | | return [self.photos objectAtIndex:index]; |
| | | } |
| | | return nil; |
| | | } |
| | | |
| | | - (void)didClickCurrentPhotoBrowserBlank:(MWPhotoBrowser *)photoBrowser { |
| | | [self.navigationController popToViewController:self animated:NO]; |
| | | } |
| | | |
| | | - (void)didClickDownLoad:(MWPhotoBrowser *)photoBrowser { |
| | | |
| | | __weak __typeof(self)weakSelf = self; |
| | | [self authPhotoLibraries:^(BOOL isAblity) { |
| | | if (isAblity) { |
| | | [weakSelf downloadTheImage]; |
| | | } else { |
| | | [weakSelf guideUserOpenAuth]; |
| | | } |
| | | }]; |
| | | } |
| | | |
| | | - (void)downloadTheImage { |
| | | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| | | if (!self.currentPreviewPhoto || self.currentPreviewPhoto.length == 0) { |
| | | [UIView dd_showMessage:NSLocalizedString(@"device_save_gallery_failure", @"下载失败")]; |
| | | return; |
| | | } |
| | | NSURL *url = [NSURL URLWithString:self.currentPreviewPhoto]; |
| | | SDWebImageManager *manager = [SDWebImageManager sharedManager]; |
| | | UIImage *img; |
| | | if ([manager diskImageExistsForURL:url]) { |
| | | img = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString]; |
| | | } else { |
| | | //从网络下载图片 |
| | | NSData *data = [NSData dataWithContentsOfURL:url]; |
| | | img = [UIImage imageWithData:data]; |
| | | } |
| | | // 保存图片到相册中 |
| | | dispatch_async(dispatch_get_main_queue(), ^{ |
| | | UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError |
| | | :contextInfo:),nil); |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | //保存图片完成之后的回调 |
| | | - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error |
| | | contextInfo:(void *)contextInfo { |
| | | // Was there an error? |
| | | NSString *message = nil; |
| | | if (!error) { |
| | | message = NSLocalizedString(@"device_save_gallery", @"已保存至手机相册"); |
| | | } |
| | | else |
| | | { |
| | | message = NSLocalizedString(@"device_save_gallery_failure", @"下载失败"); |
| | | NSLog(@"#######图片保存失败:%@", [error description]); |
| | | } |
| | | [UIView dd_showMessage:message]; |
| | | } |
| | | |
| | | /*访问相册权限 |
| | | */ |
| | | -(void)authPhotoLibraries:(void(^)(BOOL isAblity))result { |
| | | PHAuthorizationStatus author =[PHPhotoLibrary authorizationStatus]; |
| | | if (author == PHAuthorizationStatusRestricted || author ==PHAuthorizationStatusDenied){ |
| | | if (result) { |
| | | result(NO); |
| | | } |
| | | } else if (author == PHAuthorizationStatusNotDetermined) { // 首次授权 |
| | | dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); |
| | | dispatch_async(queue, ^{ |
| | | [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { |
| | | if (status != PHAuthorizationStatusAuthorized) { |
| | | if (result) { |
| | | result(NO); |
| | | } |
| | | } else { |
| | | if (result) { |
| | | result(YES); |
| | | } |
| | | } |
| | | }]; |
| | | }); |
| | | } else { |
| | | if (result) { |
| | | result(YES); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /*引导用户授权获取图片 |
| | | */ |
| | | - (void)guideUserOpenAuth{ |
| | | UIAlertController *alertC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"hdl_ez_download_tip", @"温馨提示") message:NSLocalizedString(@"hdl_ez_download_auth", @"请打开访问权限") preferredStyle:(UIAlertControllerStyleAlert)]; |
| | | UIAlertAction *alertA = [UIAlertAction actionWithTitle:NSLocalizedString(@"hdl_ez_download_cancle", @"取消") style:(UIAlertActionStyleDefault) handler:nil]; |
| | | UIAlertAction *act = [UIAlertAction actionWithTitle:NSLocalizedString(@"hdl_ez_download_toSet", @"去设置") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { |
| | | // 引导用户设置 |
| | | NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; |
| | | |
| | | if ([[UIApplication sharedApplication] canOpenURL:url]) { |
| | | |
| | | [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; |
| | | } |
| | | }]; |
| | | [alertC addAction:alertA]; |
| | | [alertC addAction:act]; |
| | | [self presentViewController:alertC animated:YES completion:nil]; |
| | | } |
| | | |
| | | #pragma mark PrivateMethod |
| | | - (void)previewPictureWithUrl:(NSString *)picUrl { |
| | | self.currentPreviewPhoto = picUrl; |
| | | self.photos = [NSMutableArray array]; |
| | | [self.photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:picUrl]]]; |
| | | MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self]; |
| | | browser.displayActionButton = NO; |
| | | browser.zoomPhotosToFill = NO; |
| | | [browser showDownloadBtn]; |
| | | |
| | | [self.navigationController pushViewController:browser animated:NO]; |
| | | } |
| | | |
| | | |
| | | @end |