iOS 打开系统相册和使用相机
来源:互联网 发布:mysql explain 编辑:程序博客网 时间:2024/05/16 12:17
使用相机
添加系统相册照片
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
0 0
- iOS 打开系统相册和使用相机
- iOS 打开系统相册和使用相机
- ios开发 使用系统相册和相机
- iOS 打开相机、相册
- iOS开发 调用系统相机和相册
- iOS 调用系统相机和相册
- iOS 系统相机和相册权限
- JS中兼容实现android和ios系统手机打开相机并可选择相册功能
- iOS 打开相册或相机
- android 打开相机和相册
- Unity与IOS交互,调用IOS系统相机和相册
- 如何优雅地使用iOS系统相机、相册
- iOS 打开相册,保存图片到相册,打开相机
- iOS调用系统相机和相册的详细用法
- iOS调用系统相册和相机选择图片
- iOS相机相册使用权限
- iOS 取相册照片/打开相机
- iOS相机和相册-UIImagePickerController
- PHP内核分析(三)-Apache
- Win8.1操作系统上PLSQL Developer无法正常使用解决方案
- Nginx深入详解之多进程网络模型
- Python读写Excel文件的实例
- 两个字符串的相关问题
- iOS 打开系统相册和使用相机
- Redhat/CentOS6.2 x86系统KVM虚拟机网络配置(一)—NAT方式
- javascript 单例模式
- HDU1241 Oil Deposits DFS
- 深度学习转载
- ios系统声音服务与震动
- NTLM加密和MD4加密
- android4.4 framework添加第三方调用接口
- Java 实用小方法