最近要作一個本地通信錄的App,須要上傳我的頭像照片,能夠本地相冊也能夠攝像頭獲取,因此參考了此篇博客,頗有用。php
http://blog.sina.com.cn/s/blog_7e102f620101dulc.htmlhtml
代碼以下 | |
#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,代表當前圖片的來源爲相冊,除此以外還能夠設置用戶對圖片是否可編輯。ios
代碼以下 | |
#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。服務器
在和用戶交互以後,用戶選擇好圖片後,會回調選擇結束的方法。框架
代碼以下 | |
//選擇完照片後掉用的方法,能夠對圖片進行設置或處理iphone - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info |
在回調結束的方法中,咱們對圖片進行了大小的處理,爲圖片的上傳作準備。函數
縮放圖片 post
代碼以下 | |
//壓縮圖片 + (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目錄下。ui
代碼以下 | |
#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下面獲取圖片,咱們首先須要獲取Documents目錄的路徑。
代碼以下 | |
#pragma mark 從文檔目錄下獲取Documents路徑 - (NSString *)documentFolderPath { return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; } |
而後,咱們即可以經過文件名,去訪問獲取資源了。
上傳圖片
項目中咱們使用了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]; } |
或者不用回掉函數,用阻塞塊。
NSString *strUrl = [[NSString alloc] initWithFormat:@"%@/setting.php",_HTTP_SERVER ];
NSURL *url = [NSURL URLWithString:strUrl];
strUrl = Nil;
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSMutableDictionary *dic = [NSMutableDictionarydictionary];
//上傳圖片
NSData *imageData = UIImagePNGRepresentation(headImage);
[request setData:imageData withFileName:@"temp22.png"andContentType:@"image/png" forKey:@"uploadImage"];//和服務器對應
postString = Nil;
[request setCompletionBlock:^{
//服務器成功返回
NSLog ( @"request.responseString = %@" ,[request responseString ]);NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:Nil];
if (dic){}
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];//異步請求;
return ;
//下載圖片:
+ (void) DownHeadImage:(PSHUserInfo *)friend
{
NSString *imageUrl = [NSStringstringWithFormat:@"%@/%@",_HTTP_SERVER,friend.userImageUrl];
NSLog(@"%@",imageUrl);
NSURL *url = [NSURL URLWithString:imageUrl];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setFailedBlock:^{
NSLog(@"下載失敗");
}];
[request setCompletionBlock:^{
UIImage *image = [UIImage imageWithData:request.responseData];
friend.userImage = image;
}];
[request startAsynchronous];
}
系統至ios6以後,關於圖片拉伸的方法已經擴展至3個函數:
1.ios4提供的方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
這個函數是UIImage的一個實例函數,它的功能是建立一個內容可拉伸,而邊角不拉伸的圖片,須要兩個參數,第一個是不拉伸區域距離左邊框的寬度,第二個參數是不拉伸區域距離上邊框的寬度,其操做本質是對一個像素的複製拉伸,故沒有漸變效果,這也是其缺點所在。
參數的意義是,若是參數指定10,5。那麼,圖片左邊10個點,上邊5個點。不會被拉伸,x座標爲11的點會被橫向複製,y座標爲6的點會被縱向複製。注意:只是對一個點像素進行復制到指定的寬度。(即劃出下圖的橢圓部分,而後非橢圓部分拉昇)
2.ios5提供的方法
- (UIImage *)resizableImageCapInsets:(UIEdgeInsets)Insets
其中Insets這個參數的格式是(top,left,bottom,right),從上、左、下、右分別在圖片上畫了一道線,這樣就給一個圖片指定了一個矩形區域。只有在框裏面的部分纔會被拉伸,而框外面的部分則保持改變。好比(20,5,10,5),意思是下圖矩形裏面的部分能夠被拉伸,而其他部分不變。
3.ios6提供的方法:
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
關於Insets參數,與ios5是相同的,不一樣的是其後增長了一個拉伸的模式,ios6.0的版本提供了
UIImageResizingModeTile和 UIImageResizingModeStretch兩種模式,從名字就能夠看出,是平鋪模式和拉伸模式。平鋪就是複製你Insets指定的矩形區域塊來填充你所指定的圖片區域(也就無漸變效果),而拉伸就是經過拉伸你Insets指定的矩形區域塊來填充你 所需的圖片區域(有漸變效果)。我想,相較4.0的進步你也看出來了,是明顯的吧,相較於之前的,圖片的resize由一個點變成了一個矩形塊,這樣你的所指定塊的漸變效果,也是能夠呈現出來的。
只是,若是你須要兼容4.0的機器的話,那麼仍是需用老的函數來完成對圖片的resize操做的。