ios--笔记--调用相机拍照及获取图片
2014-05-07 08:41
381 查看
#import "UIImage+OpenCV.h" #import "MyViewController.h" #import "OMGToast.h" #import <QuartzCore/QuartzCore.h> @interface MyViewController () - (void)processFrame; @end @implementation MyViewController @synthesize imageViewSrc = _imageViewSrc; @synthesize imageViewDesc = _imageViewDesc; @synthesize imageViewTemp = _imageViewTemp; @synthesize imgTmp = _imgTmp; @synthesize imgSrc = _imgSrc; @synthesize imgDesc = _imgDesc; - (void)viewDidLoad { [super viewDidLoad]; } -(IBAction)selectResPic:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = (id)self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //混合类型 photo + movie picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; } [self presentModalViewController:picker animated:YES]; } -(IBAction)selectDescPic:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从手机相册选择",nil]; actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView:self.view]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { //拍照 UIImagePickerController *camera = [[UIImagePickerController alloc] init]; camera.delegate = self; camera.allowsEditing = YES; //isCamera = TRUE; //检查摄像头是否支持摄像机模式 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { camera.sourceType = UIImagePickerControllerSourceTypeCamera; camera.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; } else { NSLog(@"Camera not exist"); return; } [self presentViewController:camera animated:YES completion:nil]; }else if (buttonIndex == 1) { //手机相册选择 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //混合类型 photo + movie picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; } [self presentModalViewController:picker animated:YES]; }else if(buttonIndex == 2) { //取消 } } - (void)actionSheetCancel:(UIActionSheet *)actionSheet{ } -(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ } -(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ } - (void)viewDidUnload { [super viewDidUnload]; self.imageViewSrc = nil; self.imageViewDesc=nil; delete _videoCapture; _videoCapture = nil; } #pragma mark - #pragma mark ImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [picker dismissModalViewControllerAnimated:NO]; NSLog(@"info = %@",info); NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if([mediaType isEqualToString:@"public.image"]) //被选中的是图片 { //获取照片实例 UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; NSString *fileName = [[NSString alloc] init]; if ([info objectForKey:UIImagePickerControllerReferenceURL]) { fileName = [[info objectForKey:UIImagePickerControllerReferenceURL] absoluteString]; //ReferenceURL的类型为NSURL 无法直接使用 必须用absoluteString 转换,照相机返回的没有UIImagePickerControllerReferenceURL,会报错 fileName = [self getFileName:fileName]; } else { fileName = [self timeStampAsString]; } NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults]; [myDefault setValue:fileName forKey:@"fileName"]; //to-do } -(NSString *)getFileName:(NSString *)fileName { NSArray *temp = [fileName componentsSeparatedByString:@"&ext="]; NSString *suffix = [temp lastObject]; temp = [[temp objectAtIndex:0] componentsSeparatedByString:@"?id="]; NSString *name = [temp lastObject]; name = [name stringByAppendingFormat:@".%@",suffix]; return name; } -(NSString *)timeStampAsString { NSDate *nowDate = [NSDate date]; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"EEE-MMM-d"]; NSString *locationString = [df stringFromDate:nowDate]; return [locationString stringByAppendingFormat:@".png"]; } @end
相关文章推荐
- IOS7开发~Xcode5制作framework
- 开始iOS 7中自动布局教程(一)
- iOS缓存到sandbox
- iOS缓存到内存
- 转:js判断是android访问还是ios访问
- 开发QQ互联ios版Ane扩展 辛酸史
- 总结 IOS 7 内存管理
- iostream.h找不到问题
- IOS删除tableview'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in
- iOS笔记 Collection View Controller
- IOS学习高级课程(二)coredata
- IOS protocol 的使用
- 《iOS用户体验》总结与思考-修改版
- IOS视频播放
- 实现IOS版的抽屉效果
- ios中如何拍照,录制视频,选择照片
- iOS交互之翻转屏幕
- 苹果ios开发一年的工作笔记
- ios 真机测试
- IOS修改设备用户代理UserAgent