iOS 调用系统相机和相册
来源:互联网 发布:域名映射ip和端口 编辑:程序博客网 时间:2024/05/18 19:22
一,创建button,触发点击事件,调用系统相机/相册选取头像
- (IBAction)chooseUserPicAction:(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];}
二,遵循协议
<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
三,实现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; // //iphone使用此方法进行跳转// [self presentViewController:imagePickerController animated:YES completion:^{}]; //ipad需要使用此方法进行跳转 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // Place image picker on the screen [self presentViewController:imagePickerController animated:YES completion:nil]; }]; }}
四,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.userPicImageView setImage:savedImage]; self.userPicImageView.tag = 100; }- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self dismissViewControllerAnimated:YES completion:^{}];}
五,保存图片至沙盒
- (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];
0 0
- iOS开发 调用系统相机和相册
- iOS 调用系统相机和相册
- iOS--调用相机和相册
- Unity与IOS交互,调用IOS系统相机和相册
- 调用系统相册和相机
- iOS调用系统相机和相册的详细用法
- iOS调用系统相册和相机选择图片
- 调用系统相册和调用相机
- ios开发调用系统相机相册
- [iOS]调用系统相机,相册,闪光灯
- iOS学习: 系统相机 相册的调用
- 调用系统相册和系统相机拍照
- IOS 如何调用相机和相册
- iOS app调用相机和相册
- iOS 调用相机和相册详解
- 调用系统相机相册
- 调用系统相册,相机
- 调用系统相机相册
- Powershell之MOF后门
- spring4声明式事务—02 xml配置方式
- 一些简单的操作符重载
- 责任链的方法化
- android 获取当前显示页面app 包名
- iOS 调用系统相机和相册
- openwrt之wifi驱动显示连接设备MAC
- cmd-bat批处理命令延时方法
- 观察者的前世今生
- 关于U秘开发的一些经验教训
- [Leetcode]Binary Tree Vertical Order Traversal
- jpg图片的Exif及gps信息和示例分析
- Java中BigDecimal的8种舍入模式
- eclipse中使用RecyclerView报错:Error inflating class android.support.v7.widget.RecyclerView