打开手机相机显示已为相册关闭数据网络是什么意思意思

程序写累了,就来玩玩酷跑小游戏吧,嘿嘿。
雨松MOMO送你一首歌曲,嘿嘿。
IOS研究院之打开照相机与本地相册选择图片(六)
IOS研究院之打开照相机与本地相册选择图片(六)
围观48721次
编辑日期: 字体:
Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈。 这篇文章主要学习如何在IOS程序中打开照相机与本地相册并且选择一张图片。还是老样子MOMO写了一个简单的测试程序,如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样。最终点击发送将按钮将图片2进制图片上传服务器。
下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。
AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。
#import &UIKit/UIKit.h&#import "TestViewController.h"&@interface AppDelegate : UIResponder &UIApplicationDelegate&&@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) UINavigationController *navController;@property (strong, nonatomic) UIViewController *viewController;@end
AppDelegate.m 在这里就是打开我们创建的TestViewController
12345678910111213141516171819202122232425262728
#import "AppDelegate.h"&@implementation AppDelegate&@synthesize window = _window;@synthesize navController;@synthesize viewController;&- (void)dealloc{&&&&[_window release];&&&&[super dealloc];}&- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{&&&&self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];&&&&&self.window.backgroundColor = [UIColor whiteColor];&&&&self.viewController =&&[[TestViewController alloc]init];&&&&self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];&&&&[self.window addSubview:navController.view];&&&&&[self.window makeKeyAndVisible];&&&&return YES;}&@end
TestViewController.h 注意这里面引入了很多代理类。
1234567891011121314
#import &UIKit/UIKit.h&&@interface TestViewController : UIViewController&UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate&{&&&&//输入框&&&&UITextView *_textEditor;&&&&&//下拉菜单&&&&UIActionSheet *myActionSheet;&&&&&//图片2进制路径&&&&NSString* filePath;}@end
TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
#import "TestViewController.h"&@interface TestViewController ()&@end&@implementation TestViewController&- (void)viewDidLoad{&&&&[super viewDidLoad];&&&&//导航栏标题 self.navigationItem.title = @"雨松MOMO输入框";&&&&&//导航栏按钮&&&&self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& initWithTitle: @"发送"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& style: UIBarButtonItemStyleDone&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& target: self&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& action: @selector(sendInfo)] autorelease];&&&&&//输入框显示区域&&&&_textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];&&&&//设置它的代理&&&&_textEditor.delegate = self;&&&&_textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth;&&&&_textEditor.keyboardType = UIKeyboardTypeDefault;&&&&_textEditor.font = [UIFont systemFontOfSize:20];&&&&_textEditor.text = @"请输入内容";&&&&&//默认软键盘是在触摸区域后才会打开&&&&//这里表示进入当前ViewController直接打开软键盘&&&&[_textEditor becomeFirstResponder];&&&&&//把输入框加在视图中&&&&[self.view addSubview:_textEditor];&&&&&//下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册&&&&UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]];&&&&&UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];&&&&button.frame = CGRectMake(0, 120, image.size.width, image.size.height);&&&&&[button setImage:image forState:UIControlStateNormal];&&&&&[button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];&&&&&//把它也加在视图当中&&&&[self.view addSubview:button];&}&-(void)openMenu{&&&&//在这里呼出下方菜单按钮项&&&&myActionSheet = [[UIActionSheet alloc]&&&&&&&&&&&&&&&& initWithTitle:nil&&&&&&&&&&&&&&&& delegate:self&&&&&&&&&&&&&&&& cancelButtonTitle:@"取消"&&&&&&&&&&&&&&&& destructiveButtonTitle:nil&&&&&&&&&&&&&&&& otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];&&&&&&&[myActionSheet showInView:self.view];&&&&[myActionSheet release];&&&&&}&- (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;&&&&&&&&[picker release];&&&&&&&&[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];&&&&[picker release];}&//当选择一张图片后进入这里-(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)] autorelease];&&&&&&&&&&&&&smallimage.image = image;&&&&&&&&//加在视图中&&&&&&&&[self.view addSubview:smallimage];&&&&&} &}&- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{&&&&NSLog(@"您取消了选择图片");&&&&[picker dismissModalViewControllerAnimated:YES];}&-(void)sendInfo{&&&&NSLog(@"图片的路径是:%@", filePath);&&&&&NSLog(@"您输入框中的内容是:%@", _textEditor.text);}&- (void)viewDidUnload{&&&&[super viewDidUnload];&&&&// Release any retained subviews of the main view.}&- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{&&&&return (interfaceOrientation == UIInterfaceOrientationPortrait);}&@end
如下图所示,打开下拉菜单按钮开始选择打开相机 或者 打开本地相册。模拟器中是无法打开照相机的的,切记。
如下图所示,这里就是我本地的相册啦,里面保存了几张图片,选择一张即可。
我在这里再说说图片上传, 图片上传我们采用的是2进制ASIHTTPRequest 来完成的。
123456789101112131415
&&&&NSString *server_base = [NSString stringWithFormat:@"%@/users/uploadResource.json", _server];&&&&&ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];&&&&&&&ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:server_base]];&&&&&[ASIHTTPRequest setShouldUpdateNetworkActivityIndicator: NO];&&&&[request setDelegate :self];&&&&[request setDidFinishSelector:@selector(sendCommentSucc:)];&&&&[request setDidFailSelector:@selector(sendCommentFail:)];&&&&// res 就是 需要上传图片文件的路径&&&&[request setFile:res forKey:@"res"];&&&&&[queue addOperation:request];&&&&[queue go];
最后是文本的源码,雨松MOMO祝大家学习愉快,不早了,我也得睡觉啦,1点多了。。。
下载地址:
本文固定链接:
转载请注明:
MOMO与MO嫂提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
您可能还会对这些文章感兴趣!用三星手机照相,一按快门就自动调到相册里-中国学网-中国IT综合门户网站
> 信息中心 >
用三星手机照相,一按快门就自动调到相册里
来源:互联网 发表时间: 10:17:34 责任编辑:王亮字体:
为了帮助网友解决“用三星手机照相,一按快门就自动调到相册里”相关的问题,中国学网通过互联网对“用三星手机照相,一按快门就自动调到相册里”相关的解决方案进行了整理,用户详细问题包括:RT,我想知道:用三星手机照相,一按快门就自动调到相册里去了,怎么回事。。。以前都不是这样的,怎么设置回去阿,,。,具体解决方案如下:解决方案1:   您好,感谢您选择三星产品。  根据您的描述,如您使用的是安卓系统的手机,则建议您按照如下步骤尝试操作:  1.相机-照相模式-设定-重置-重置照相机设定  2.设置-应用程序-全部-点击相机-清除数据  3.备份手机数据(电话薄、短信信息、多媒体资料等)恢复手机出厂设置:设置-隐私权-恢复出厂设置  若问题依然存在,建议您联系三星服务中心进行检测,具体服务中心地址及联系电话请您访问:(建议您去前先联系服务中心)  /cn/support/location/supportServiceLocation.do?page=SERVICE.LOCATION&cid=cn_ppc_support_service_repairnet_120522  欢迎登陆三星数字服务平台提问:.cn/ask/  感谢您的支持,祝您生活愉快!
1个回答1个回答1个回答1个回答1个回答2个回答1个回答1个回答1个回答1个回答
相关文章:
最新添加资讯
24小时热门资讯
Copyright © 2004- All Rights Reserved. 中国学网 版权所有
京ICP备号-1 京公网安备02号手机数据上网是什么意思 关掉用WIFI上网影响吗?_百度知道
手机数据上网是什么意思 关掉用WIFI上网影响吗?
1,手机数据上网是用流量上网的意思,流量是自己花钱向移动或者联通等等购买的一种上网需要用到的东西。2,关掉数据对WiFi上网没影响,WiFi是不用流量上网,用WiFi可以节省流量,如果有WiFi,尽量用WiFi上网,流量用不完的除外。
其他类似问题
为您推荐:
是用流量上网。,完全可以关掉用WIFI上网。
wifi上网的相关知识
其他2条回答
手机数据上网是指用手机卡上网,关掉不影响WIFI
不影响的,不过数据上网比较费钱
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信