// // PicturesTableViewCell.m // WaterProofer // // Created by admin on 2019/9/21. // Copyright © 2019 WaterProofer. All rights reserved. // #import "PicturesTableViewCell.h" @import AVFoundation; @import Photos; @interface PicturesTableViewCell ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> @end @implementation PicturesTableViewCell - (void)awakeFromNib { [super awakeFromNib]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } - (IBAction) cameraClick: (UIButton * ) SENDER { // call the album, camera [self createCameraOrPhotoAlbum]; } #pragma Mark - call camera, photo album - (void)createCameraOrPhotoAlbum { __weak typeof(self)wself = self; UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil Message: nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [wself createPhotoAlbum]; }]; [alert addAction:action]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [wself createCamera]; }]; [alert addAction:action1]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:cancel]; [self.containingViewController presentViewController:alert animated:YES completion:nil]; } - (void)createCamera { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限 if (granted) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self.containingViewController presentViewController:picker animated:YES completion:^{ }]; }else{ [Self showRequestErrorTitle: @ " Please turn the camera permission " Handler: ^ (* UIAlertAction Action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; } }]; } #pragma - Mark - Select the image proxy method - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //得到图片 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; UIImage *newImage = [self croppedImage:image]; // stored in albums IF (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { // stored album UIImageWriteToSavedPhotosAlbum (newImage, nil, nil, nil); } else if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) { } [Self uploadImageWithImagePath: newimage]; // to save the picture - "Upload [self.containingViewController dismissViewControllerAnimated: YES Completion: ^ { }]; } // Crop Picture - (UIImage *) croppedImage: (UIImage * ) Image { IF (Image) { // solve the problem but rather UIGraphicsBeginImageContext (image.size); [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //裁剪 CGRect rectMAX = CGRectMake((newImage.size.width-newImage.size.height)/2, 0, newImage.size.height, newImage.size.height); CGImageRef subImageRef = CGImageCreateWithImageInRect(newImage.CGImage, rectMAX); UIGraphicsBeginImageContext(rectMAX.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextDrawImage(context, CGRectMake(0, 0, newImage.size.height, newImage.size.height), subImageRef); UIImage *viewImage = [UIImage imageWithCGImage:subImageRef]; UIGraphicsEndImageContext(); CGImageRelease(subImageRef); return viewImage; } return nil; } - (void)createPhotoAlbum { [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized){ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self.containingViewController presentViewController:picker animated:YES completion:^{ }]; }else{ [Self showRequestErrorTitle: @ " Please open the album authority " Handler: ^ (* UIAlertAction Action) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; }]; // UIAlertController * Alert = // [UIAlertController alertControllerWithTitle: @ "Please open the album authority" // the Message: @ "to open the path: Settings> Privacy> Photos> tomato" // preferredStyle: UIAlertControllerStyleAlert]; // // UIAlertAction * Action = // [UIAlertAction actionWithTitle: @ "OK" // style: UIAlertActionStyleDefault // Handler: nil]; // // [alert addAction:action]; // [self presentViewController:alert animated:YES completion:nil]; } }]; } #pragma Mark - upload an avatar - (void)uploadImageWithImagePath:(UIImage *)image { [self.cameraBtn setImage:image forState:UIControlStateNormal]; __weak typeof(self)weakSelf = self; [[WPApiRequest sharedManager] PostBodyRequestWithActionName:@"" andPramater:@{} andImage:@"" succeeded:^(id _Nonnull responseObject) { }]; } @end
// upload pictures - ( void ) uploadMostImageWithURLString: (NSString * ) URLString parameters:(NSDictionary*)parameters uploadDatas:(NSArray *)uploadDatas uploadName:(NSString *)uploadName success:(void (^)(NSDictionary*dic))success{ // 1. Manager AFHTTPSessionManager Manager * = [AFHTTPSessionManager Manager]; // 2 set type manager.requestSerializer = [AFHTTPRequestSerializer Serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil]; manager.requestSerializer.timeoutInterval = 20.0f; //超时时间 [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"app_system"]; NSMutableDictionary* postDic = [[NSMutableDictionary alloc]init]; if (parameters) { [postDic addEntriesFromDictionary:parameters]; } [postDic setObject:[ArtLoginData sharedManager].cityID forKey:@"city_id"]; [postDic setObject:[ArtLoginData sharedManager].tokenStr forKey:@"token"]; [postDic setObject:@"2" forKey:@"vtype"]; if (![[parameters allKeys]containsObject:@"phone"]) { [postDic setObject:[ArtLoginData sharedManager].userID forKey:@"phone"]; } if (!(uploadDatas.count>0)) { [postDic setObject:@"" forKey:@"imageurl"]; } NSString *stringurl=[NSString stringWithFormat:@"%@%@",BASEURL,URLString]; [self showHUD]; [manager POST:stringurl parameters:postDic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData){ /* * Parameters of the method 1. appendPartWithFileData: To upload photos [binary stream] 2. name: corresponding to the site [in, upload.php,] field processing of documents (such as Upload) 3. fileName: To save the file on the server name Type of uploaded files: 4. mimeType */ for (int i = 0; i < uploadDatas.count; i++){ UIImage *image = uploadDatas[i]; NSData *imageData = UIImageJPEGRepresentation(image, 0.8); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyyMMddHHmmss"]; NSString *dateString = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", dateString]; [formData appendPartWithFileData:imageData name:uploadName fileName:fileName mimeType:@"image/jpeg"]; } }progress:^(NSProgress * _Nonnull uploadProgress) { }success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { [self hideHUD]; ResultModel *model=[ResultModel yy_modelWithDictionary:responseObject]; if (model.isSuccess) { if(success){ success(responseObject); } }else{ [self showToastWithText:model.msg]; } }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [self hideHUD]; if (TBReachability){ [Self showToastWithText: @ " Connection to server failed! " ]; }else{ [Self showToastWithText: @ " Check your network connection! " ]; } }]; }
<key>NSCameraUsageDescription</key>
<String> XXXAPP upload pictures of service required to access your camera </ string>
<key>NSPhotoLibraryUsageDescription</key>
<string> XXXAPP services in upload pictures need to access your album </ string>