iOS調用系統功能與跳轉到系統設置http://www.jianshu.com/p/78db0e46d954
關於iOS中簡單實現調用系統相機及相冊功能http://www.jianshu.com/p/e70a184d1f32ios
iOS 圖片壓縮UIImage方法擴展http://blog.csdn.net/justinjing0612/article/details/8751269
ios中攝像頭/相冊獲取圖片,壓縮圖片,上傳服務器方法總結http://blog.csdn.net/justinjing0612/article/details/8751220
這幾天在搞iphone上面一個應用的開發,裏面有須要攝像頭/相冊編程和圖片上傳的問題,在這裏總結一下。編程
【部分知識】服務器
iphone中圖像一般存儲在4個地方【相冊、應用程序包、沙盒、Internet】,經過這4個源,咱們就能夠存取應用圖片。app
iphone的相冊包含攝像頭膠捲+用戶計算機同步的部分照片。用戶能夠經過UIImagePickerController類提供的交互對話框來從相冊中選擇圖像。可是,注意:相冊中的圖片機器路徑沒法直接從應用程序訪問,只能經過終端用戶去選擇和使用相冊圖片框架
應用程序包可能會將圖像與可執行程序、Info.plist文件和其餘資源一同存儲。咱們能夠經過本地文件路徑來讀取這些基於包的圖像並在應用程序中顯示它們。iphone
藉助沙盒,咱們能夠把圖片存儲到Documents、Library、tmp文件夾中。這些文件都可有應用程序讀取,且能夠經過文件路徑建立圖像。儘管沙盒外的部分從技術上說是可行的,可是apple代表這些部分不在appstore應用程序容許訪問的範圍以內。ui
應用程序能夠經過圖片的URL來訪問Internet上的資源。this
以上爲一些小知識,來自《iphone開發祕籍(第二版)》,能夠本身去參考此書。atom
下面開始切入正題,從攝像頭/相冊獲取圖片,壓縮圖片,上傳圖片。url
剛剛在上面的知識中提到從攝像頭/相冊獲取圖片是面向終端用戶的,由用戶去瀏覽並選擇圖片爲程序使用。在這裏,咱們須要過UIImagePickerController類來和用戶交互。
使用UIImagePickerController和用戶交互,咱們須要實現2個協議<UIImagePickerControllerDelegate,UINavigationControllerDelegate>。
#pragma mark 從用戶相冊獲取活動圖片 - (void)pickImageFromAlbum { imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing = YES; [self presentModalViewController:imagePicker animated:YES]; }
咱們來看看上面的從相冊獲取圖片,咱們首先要實例化UIImagePickerController對象,而後設置imagePicker對象爲當前對象,設置imagePicker的圖片來源爲UIImagePickerControllerSourceTypePhotoLibrary,代表當前圖片的來源爲相冊,除此以外還能夠設置用戶對圖片是否可編輯。
#pragma mark 從攝像頭獲取活動圖片 - (void)pickImageFromCamera { imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing = YES; [self presentModalViewController:imagePicker animated:YES]; }
以上是從攝像頭獲取圖片,和從相冊獲取圖片只是圖片來源的設置不同,攝像頭圖片的來源爲UIImagePickerControllerSourceTypeCamera。
在和用戶交互以後,用戶選擇好圖片後,會回調選擇結束的方法。
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"]; if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) { // UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); } theImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(120.0, 120.0)]; UIImage *midImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)]; UIImage *bigImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 440.0)]; [theImage retain]; [self saveImage:theImage WithName:@"salesImageSmall.jpg"]; [self saveImage:midImage WithName:@"salesImageMid.jpg"]; [self saveImage:bigImage WithName:@"salesImageBig.jpg"]; [self dismissModalViewControllerAnimated:YES]; [self refreshData]; [picker release]; }
在回調結束的方法中,咱們對圖片進行了大小的處理,爲圖片的上傳作準備。
縮放圖片比較簡單,就直接放上代碼,讓你們參考一下。
//壓縮圖片 + (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize { // Create a graphics image context UIGraphicsBeginImageContext(newSize); // Tell the old image to draw in this new context, with the desired // new size [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; }
在上面咱們獲取到了圖片並對圖片進行了壓縮,經過以前的小知識瞭解到,將應用須要的一些圖片存入沙盒是個不錯的選擇,並且應用程序能夠直接經過路徑去方法沙盒中的圖片,在這裏咱們將圖片存入沙盒中的Documents目錄下。
#pragma mark 保存圖片到document - (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName { NSData* imageData = UIImagePNGRepresentation(tempImage); NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; // Now we get the full path to the file NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; // and then we write it out [imageData writeToFile:fullPathToFile atomically:NO]; }
要從Documents下面獲取圖片,咱們首先須要獲取Documents目錄的路徑。
#pragma mark 從文檔目錄下獲取Documents路徑 - (NSString *)documentFolderPath { return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; }
而後,咱們即可以經過文件名,去訪問獲取資源了。
View Code
項目中咱們使用了ASIFormHttpRequest的開源框架,http請求的部分代碼以下,http返回以及相關回調方法略去。
- (void)upLoadSalesBigImage:(NSString *)bigImage MidImage:(NSString *)midImage SmallImage:(NSString *)smallImage { NSURL *url = [NSURL URLWithString:UPLOAD_SERVER_URL]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:@"photo" forKey:@"type"]; [request setFile:bigImage forKey:@"file_pic_big"]; [request buildPostBody]; [request setDelegate:self]; [request setTimeOutSeconds:TIME_OUT_SECONDS]; [request startAsynchronous]; }