iOS開發中,常常須要咱們上傳圖片到服務器,直接上代碼。服務器
#import "ViewController.h"app
#import "AFNetworking.h"post
/**代理
* 遵照UIImagePickerControllerDelegate代理方法code
*/orm
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>對象
@end接口
@implementation ViewController圖片
- (void)viewDidLoad {ip
[super viewDidLoad];
}
- (IBAction)postImg:(UIButton *)sender
{
UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消 " destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"選取照片", nil];
/**
* 設置actinsheet的樣式
*/
[actionSheet setActionSheetStyle:UIActionSheetStyleDefault];
/**
* 顯示actionsheet
*/
[actionSheet showInView:self.view];
}
#pragma mark -- UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
{
/**
* 打開照相機,先判斷相機能不能打開,isSourceTypeAvailable後面是三個枚舉值。UIImagePickerControllerSourceTypeCamera表示相機可用。
*/
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
/**
* picker.delegate 有兩個,UINavigationControllerDelegate和UIImagePickerControllerDelegate
*/
picker.delegate = self;
/**
* 設置拍照以後是否可編輯,若是設置成可編輯的話會在代理方法返回的字典裏面多一些鍵值。
*/
picker.allowsEditing = YES;
/**
* 調用系統相機,UIImagePickerControllerSourceTypeCamera表示調用相機。UIImagePickerControllerSourceTypePhotoLibrary表示照片庫
*/
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentViewController:picker animated:YES completion:nil];
}else{
/**
* 不然,相機不可用。
*/
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"沒有攝像頭" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
[alert show];
}
}
break;
case 1:
{
/**
* 打開圖片庫。先判斷照片庫是否可用
*/
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
/**
* 和前面設置同樣
*/
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}else{
/**
* 不然,圖片庫不能用。
*/
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"圖片庫不可用" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
[alert show];
}
}
break;
default:
break;
}
}
#pragma mark -- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
/**
* 點擊選擇時,會觸發此方法。圖片全部的信息,都包含在info中,能夠打印看一下。打印能夠看出,圖片的key值是UIImagePickerControllerEditedImage
*/
UIImage * img = [info objectForKey:UIImagePickerControllerEditedImage];
/**
* PS:此時UIImagePickerControllerEditedImage是一個宏,因此[info objectForKey:UIImagePickerControllerEditedImage]和[info objectForKey:@"UIImagePickerControllerEditedImage"]是等價的。
*/
/**
* 若是圖片是拍照得來的,那麼要存入圖片庫。
*/
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);//把圖片存到圖片庫
}
/**
* 把圖片轉壞二進制圖片,由於不知道是ipg格式仍是png格式,因此要進行判斷。
*/
NSData * data;
if (UIImagePNGRepresentation(img) == nil) {
/**
* 把圖片轉化爲二進制
*
* @param img 圖片
* @param compressionQuality#> 圖片的質量,可填0到1之間的數,1的質量最好 description#>
*
* @return 返回二進制數據
*/
data = UIImageJPEGRepresentation(img, 1);//若是png轉化爲二進制爲0,則圖片確定是jpg格式的。
}else{
data = UIImagePNGRepresentation(img);
}
/**
* 若是須要把圖片存入沙河,則執行下面的操做
*/
/**
* ///////////////////////////////////////////////////
*/
/**
* 獲取Documents路徑
*/
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
/**
* 建立文件管理器
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
/**
* 把剛剛圖片轉換的data對象拷貝至沙盒中 並保存爲image.png
*/
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
/**
* 下面這一句的意思是先把Documents/image.png路徑下的文件刪除,
*/
[fileManager removeItemAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
/**
* ////////////////////////////////////////////////
*/
/**
* 獲取到圖片,而後上傳。
*/
/**
* 上傳以前先將圖片轉化成base64,做爲一個參數,看後臺須要什麼參數。
*/
NSString * imgStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
[manager POST:@"後臺接口" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
/**
* appendPartWithFileData : 上傳的二進制數據
* name : 服務器存放圖片的文件路徑
* fileName : 上傳後文件的名字。
* mimeType : 文件類型,圖片就是@"image/jpg" ;
這幾個參數不絕對,看後臺怎麼作接口。
*/
[formData appendPartWithFileData:data name:@"" fileName:@"" mimeType:@""];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"失敗");
}];
}
若有不對的地方,還請批評指正。