国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS學習:調用相機,選擇圖片上傳,帶預覽功能

2019-11-08 00:26:43
字體:
來源:轉載
供稿:網友

一、新建工程

二、拖控件,創建映射

三、在.h中加入delegate

@interface ViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

四、實現按鈕事件

-(IBAction)chooseImage:(id)sender { UIActionSheet *sheet;

// 判斷是否支持相機

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"從相冊選擇", nil]; } else { sheet = [[UIActionSheet alloc] initWithTitle:@"選擇" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"從相冊選擇", nil]; } sheet.tag = 255; [sheet showInView:self.view]; }

五、實現actionSheet delegate事件

判斷是否支持相機,跳轉到相機或相冊界面

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if (actionSheet.tag == 255) { NSUInteger sourceType = 0; // 判斷是否支持相機 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { switch (buttonIndex) { case 0: // 取消 return; case 1: // 相機 sourceType = UIImagePickerControllerSourceTypeCamera; break; case 2: // 相冊 sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break; } } else { if (buttonIndex == 0) { return; } else { sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; } }

// 跳轉到相機或相冊頁面

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self PResentViewController:imagePickerController animated:YES completion:^{}]; [imagePickerController release]; }}六、實現ImagePicker delegate 事件#pragma mark - image picker delegte- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ [picker dismissViewControllerAnimated:YES completion:^{}]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; /* 此處info 有六個值 * UIImagePickerControllerMediaType; // an NSString UTTypeImage) * UIImagePickerControllerOriginalImage; // a UIImage 原始圖片 * UIImagePickerControllerEditedImage; // a UIImage 裁剪后圖片 * UIImagePickerControllerCropRect; // an NSValue (CGRect) * UIImagePickerControllerMediaURL; // an NSURL * UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework * UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo */ // 保存圖片至本地,方法見下文 [self saveImage:image withName:@"currentImage.png"]; NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"]; UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath]; isFullScreen = NO; [self.imageView setImage:savedImage]; self.imageView.tag = 100; }- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self dismissViewControllerAnimated:YES completion:^{}];}

七、保存圖片高保真壓縮圖片方法

NSData * UIImageJPEGRepresentation ( UIImage *image, CGFloat compressionQuality)此方法可將圖片壓縮,但是圖片質量基本不變,第二個參數即圖片質量參數。 #pragma mark - 保存圖片至沙盒- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName{ NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); // 獲取沙盒目錄 NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; // 將圖片寫入文件 [imageData writeToFile:fullPath atomically:NO];}

八、實現點擊圖片預覽功能,滑動放大縮小,帶動畫

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ isFullScreen = !isFullScreen; UITouch *touch = [touches anyObject]; CGPoint touchPoint = [touch locationInView:self.view]; CGPoint imagePoint = self.imageView.frame.origin; //touchPoint.x ,touchPoint.y 就是觸點的坐標 // 觸點在imageView內,點擊imageView時 放大,再次點擊時縮小 if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <= touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y) { // 設置圖片放大動畫 [UIView beginAnimations:nil context:nil]; // 動畫時間 [UIView setAnimationDuration:1]; if (isFullScreen) { // 放大尺寸 self.imageView.frame = CGRectMake(0, 0, 320, 480); } else { // 縮小尺寸 self.imageView.frame = CGRectMake(50, 65, 90, 115); } // commit動畫 [UIView commitAnimations]; } }九、上傳圖片,使用ASIhttpRequest類庫實現,由于本文重點不是網絡請求,故不對ASIHttpRequest詳細講述,只貼出部分代碼ASIFormDataRequest *requestReport = [[ASIFormDataRequest alloc] initWithURL:服務器地址]; NSString *Path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"]; [requestReport setFile:Path forKey:@"picturepath"];[requestReport buildPostBody];requestReport.delegate = self;[requestReport startAsynchronous];

效果圖如下:

          ->

      

轉 https://my.oschina.net/joanfen/blog/134677


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华容县| 拉萨市| 华坪县| 保定市| 清徐县| 长岭县| 茂名市| 锡林浩特市| 弋阳县| 龙南县| 大埔区| 镇远县| 波密县| 南通市| 二连浩特市| 小金县| 巫山县| 灌南县| 利津县| 通许县| 轮台县| 上饶市| 平乐县| 车险| 延吉市| 临沧市| 拉孜县| 安仁县| 大方县| 湄潭县| 罗田县| 大姚县| 兖州市| 余干县| 晋州市| 普陀区| 涿鹿县| 伽师县| 永顺县| 南昌县| 金门县|