iOS 打开系统相册和使用相机
2015-03-11 15:12
405 查看
使用相机
添加系统相册照片
ViewController.h
代码:
ViewController.m
代码:
添加系统相册照片
ViewController.h
代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate> { //输入框 UITextView *_textEditor; //下拉菜单 UIActionSheet *myActionSheet; //图片2进制路径 NSString* filePath; } @end
ViewController.m
代码:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [[UIButton alloc]init]; button.frame = CGRectMake(0, 120, 80, 80); button.backgroundColor = [UIColor redColor]; [button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; float i = [self fileSizeForDir:NSHomeDirectory()]; NSLog(@"%f", i); } -(void)openMenu { myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles: @"打开照相机", @"从手机相册获取",nil]; [myActionSheet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //呼出的菜单按钮点击后的响应 if (buttonIndex == myActionSheet.cancelButtonIndex) { NSLog(@"取消"); } switch (buttonIndex) { case 0: //打开照相机拍照 [self takePhoto]; break; case 1: //打开本地相册 [self LocalPhoto]; break; } } //开始拍照 -(void)takePhoto { UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; //设置拍照后的图片可被编辑 // picker.allowsEditing = YES; picker.sourceType = sourceType; [self presentModalViewController:picker animated:YES]; }else { NSLog(@"模拟其中无法打开照相机,请在真机中使用"); } } //打开本地相册 -(void)LocalPhoto { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; //设置选择后的图片可被编辑 // picker.allowsEditing = YES; [self presentModalViewController:picker animated:YES]; } //当选择一张图片后进入这里 -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; //当选择的类型是图片 if ([type isEqualToString:@"public.image"]) { //先把图片转成NSData UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image, 1.0); } else { data = UIImagePNGRepresentation(image); } //图片保存的路径 //这里将图片放在沙盒的documents文件夹中 NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; //得到选择后沙盒中图片的完整路径 filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"]; //关闭相册界面 [picker dismissModalViewControllerAnimated:YES]; //创建一个选择后图片的小图标放在下方 //类似微薄选择图后的效果 UIImageView *smallimage = [[UIImageView alloc] initWithFrame: CGRectMake(50, 120, 40, 40) ]; smallimage.image = image; //加在视图中 [self.view addSubview:smallimage]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"您取消了选择图片"); [picker dismissModalViewControllerAnimated:YES]; } -(void)sendInfo { NSLog(@"图片的路径是:%@", filePath); } - (float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小 { NSFileManager *fileManager = [[NSFileManager alloc] init]; float size =0; NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil]; for(int i = 0; i<[array count]; i++) { NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]]; BOOL isDir; if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) ) { NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil]; size+= fileAttributeDic.fileSize/ 1024.0/1024.0; } else { [self fileSizeForDir:fullPath]; } } return size; } - (void)viewDidUnload { [super viewDidUnload]; // any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } // Do any additional setup after loading the view, typically from a nib. - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- iOS 打开系统相册和使用相机
- JS中调用android和ios系统手机打开相机并可选择相册功能
- iOS使用UIImagePickerController调用系统相机、相册与图库
- ios开发 使用系统相册和相机
- 如何优雅地使用iOS系统相机、相册
- JS中兼容实现android和ios系统手机打开相机并可选择相册功能
- iOS开发中使用相机或系统相册的一些问题总结(UIImagePickerController)
- IOS开发笔记之十四——使用系统相册或相机导致状态栏隐藏的问题(bug总结四)
- iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏
- IOS开发调用系统相机和打开闪光灯
- 【Android】使用系统相机或者从相册中获取图片资源并进行适当裁剪
- Qt for iOS:使用UIImagePickerController获取一个系统相册中的图片
- IOS开发调用系统相机和打开闪光灯
- iOS开发--调用系统相册、相机显示中文title
- IOS开发调用系统相机和打开闪光灯
- ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像(iphone版本)
- IOS打开系统相机的闪光灯
- IOS开发调用系统相机和打开闪光灯
- iOS图片处理(一)————调用系统相机和相册获取图片,给相机添加自定义覆盖物
- ios开发调用系统相机相册