上传图片两种操作方式:一种是从相册上传,一种是拍照上传
1.UIImagePickerController在这个代理方法中做如下处理:
#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self uploadImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// bug fixes: UIIMagePickerController使用中偷换StatusBar颜色的问题
if ([navigationController isKindOfClass:[UIImagePickerController class]] &&
((UIImagePickerController *)navigationController).sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
[navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
2.调接口上传图片,图片必须转为NSData 通过这个方法 NSData *data = UIImageJPEGRepresentation(image, .5f);
上传接口如下:
- (void)uploadImage:(UIImage *)image
{
[MLProgressView showProgressViewAddedTo:self.view displayMessage:@"请稍后..." withType:0];
NSData *data = UIImageJPEGRepresentation(image, .5f);
XXXXNetRequestData *req = [[XXXXNetRequestData alloc] init];
[req setData:data forKey:@"imageFile"];
[req setString:@"0" forKey:@"imageType"];
[[网络请求类 sharedClient] requestPostWithType:XXXXRequest_uploadImage
args:[req toDictionary]
success:^(id responseObject) {
[MLProgressView hideAllHUDsForView:self.view];
NSString *url = [responseObject objectForKey:@"url"];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"sendImgLink('%@')",url]];
} failure:^(NSError *error) {
[MLProgressView hideAllHUDsForView:self.view];
Alert(@"", @"上传失败");
}];
}
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"sendImgLink('%@')",url]]; 这个端代码是涉及到与HTML交互,在HTML网页上来完成上传。