AFNetworking 上传图片 (相册、拍照,并且图片可裁剪)


将本地图片上传网络,并且可以在上传之前对本地的图片进行裁剪


@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");

    }];

}



注意: 上传照片的时候,由于本地的图片像素较高,可能会耗时,可以采用将图片压缩后再进行上传,  如何压缩,请看下一篇 “压缩图片” 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值