//如果想要調(diào)用當(dāng)前設(shè)備的相機(jī)功能(拍照/相冊/錄像),需要遵守兩個協(xié)議UIImagePickerControllerDelegate,UINavigationControllerDelegate
//從相冊獲取圖片步驟:
//創(chuàng)建當(dāng)前的相機(jī)對象
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
//設(shè)置相機(jī)的資源類型(拍照/相冊)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//相冊
//設(shè)置代理
picker.delegate = self;
/**
- 協(xié)議方法
- @param picker 相機(jī)對象
- @param info 選中的圖片的圖片信息
*/
-
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通過info獲取選中的圖片
UIImage * image = info[UIImagePickerControllerOriginalImage];
//向服務(wù)器上傳圖片,圖片必須轉(zhuǎn)換為nsdata
NSData * data = UIImagePNGRepresentation(image);
//上傳數(shù)據(jù)
//非二進(jìn)制的數(shù)據(jù) 需要使用字典封裝成請求體
//二進(jìn)制數(shù)據(jù) 要單獨(dú)拼接
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<AFMultipartFormData> formData) {
//formData 將二進(jìn)制數(shù)據(jù)拼接到此處
/**
*
* @param NSData 上傳的二進(jìn)制對象
@name 上傳的二進(jìn)制數(shù)據(jù)對應(yīng)的參數(shù)值
@fileName 上傳的資源名稱 任意 但不能帶有中文 規(guī)范寫法是要帶有資源后綴的
* @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];
}