相机

//如果想要调用当前设备的相机功能(拍照/相册/录像),需要遵守两个协议UIImagePickerControllerDelegate,UINavigationControllerDelegate
//从相册获取图片步骤:
//创建当前的相机对象
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
//设置相机的资源类型(拍照/相册)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//相册
//设置代理
picker.delegate = self;
/**

  • 协议方法
  • @param picker 相机对象
  • @param info 选中的图片的图片信息
    */
  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //通过info获取选中的图片
    UIImage * image = info[UIImagePickerControllerOriginalImage];
    //向服务器上传图片,图片必须转换为nsdata
    NSData * data = UIImagePNGRepresentation(image);
    //上传数据
    //非二进制的数据 需要使用字典封装成请求体
    //二进制数据 要单独拼接
    NSDictionary * dic = @{@"m_auth":self.authToken,@"albumid":@(0),@"pic_title":@"image_png"};

    AFHTTPRequestOperationManager * manage = [AFHTTPRequestOperationManager manager];

    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manage POST:UPLOADPATH parameters:dic constructingBodyWithBlock:^(id formData) {
    //formData 将二进制数据拼接到此处
    /**
    *
    * @param NSData 上传的二进制对象
    @name 上传的二进制数据对应的参数值
    @fileName 上传的资源名称 任意 但不能带有中文 规范写法是要带有资源后缀的
    * @mimeType 上传图片固定的类型 image/png
    * @
    */
    [formData appendPartWithFileData:data name:@"attach" fileName:@"asdf.png" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

      NSLog(@"%@",responseObject[@"message"]);
    

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@",error.description);
    }];

    [picker dismissViewControllerAnimated:YES completion:nil];

}

你可能感兴趣的:(相机)