将本地图片上传网络,并且可以在上传之前对本地的图片进行裁剪
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[selfUesrImageClicked];
}
- (void)UesrImageClicked
{
UIActionSheet *sheet;
//判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
sheet = [[UIActionSheetalloc] initWithTitle:@"选择图像"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];
}
else {
sheet = [[UIActionSheetalloc] initWithTitle:@"选择图像"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];
}
sheet.tag =255;
[sheetshowInView:self.view];
}
#pragma mark - action sheet delegte
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag ==255) {
NSUInteger sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
// 判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case 0:
return;
case 1: //相机
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 2: //相册
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else {
if (buttonIndex == 0) {
return;
}else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc] init];
imagePickerController.delegate =self;
imagePickerController.allowsEditing =YES;
imagePickerController.sourceType = sourceType;
[selfpresentViewController:imagePickerController animated:YES completion:^{}];
}
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:^{}];
//获取裁剪后的image
UIImage *image = [infoobjectForKey:UIImagePickerControllerEditedImage];
_imageView.image = image;
//图片变成data
NSData *data;
if (UIImagePNGRepresentation(image) ==nil) {
data =UIImageJPEGRepresentation(image, 1);
}else {
data = UIImagePNGRepresentation(image);
}
//上传图片
[selfpostUpLoad:data];
// 图片保存 如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//保存照片成功后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo{
NSLog(@"saved..");
}
//开始上传
-(void)postUpLoad:(NSData *)data
{
NSString * urlString =@“上传图片的服务器地址”;
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManagermanager];
[managerPOST:urlString parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//上传文件
/*
fileName 在服务器中的名称
mimeType 上传文件的类型,格式:"类型/小类" image/jpg不会的话用百度
*/
[formData appendPartWithFileData:data name:@"HeadFile"fileName:[NSStringstringWithFormat:@"%@.png",@"image"] mimeType:@"image/png"];
}success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"success");
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"failure");
}];
}
注意: 上传照片的时候,由于本地的图片像素较高,可能会耗时,可以采用将图片压缩后再进行上传, 如何压缩,请看下一篇 “压缩图片”