1.选择图片获取方式
-(void)doChange{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
__weak typeof(self) weakSelf = self;
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//判断是否可以打开相机,模拟器无法使用此功能
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
UIImagePickerController *pic = [[UIImagePickerController alloc]init];
pic.sourceType = UIImagePickerControllerSourceTypeCamera;
pic.delegate = (id)weakSelf;
pic.editing = YES;
pic.allowsEditing = YES;
[weakSelf presentViewController:pic animated:YES completion:nil];
}
}];
UIAlertAction *album = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *pic = [[UIImagePickerController alloc]init];
pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pic.delegate = (id)weakSelf;
pic.editing = YES;
pic.allowsEditing = YES;
pic.editing = YES;
[weakSelf presentViewController:pic animated:YES completion:nil];
}];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
//修改单个按钮字体颜色
camera.textColor = [UIColor hs_colorWithHex:0x5e5163];
album.textColor = [UIColor hs_colorWithHex:0x5e5163];
action.textColor = [UIColor hs_colorWithHex:0x9a9a9a];
[alertController addAction:camera];
[alertController addAction:album];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - UIImagePickerController的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
//获得选中图片
_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//裁剪image的尺寸
CGSize imagesize = _image.size;
imagesize.height = 400;
imagesize.width = 400;
_image = [self imageWithImage:_image scaledToSize:imagesize];
[self.sessionManager POST:URLStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
- (AFHTTPSessionManager *)sessionManager {
if (_sessionManager == nil) {
_sessionManager = [AFHTTPSessionManager manager];
_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
_sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSSet *set = [NSSet setWithObjects:@"text/plain", @"text/html", nil];
_sessionManager.responseSerializer.acceptableContentTypes = [_sessionManager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:set];
}
return _sessionManager;
}
对图片尺寸进行压缩
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
保存图片至沙盒
-(void)saveImage:(UIImage *)currenrImage WithName:(NSString *)imageName{
NSData *imageData = UIImageJPEGRepresentation(currenrImage, 1);
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPath atomically:NO];
}
网友评论