打开相机代码
打开相册代码
选择照片代码
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- UIImagePickerController *picker = [[UIImagePickerController alloc]init];
- picker.delegate = self;
- picker.allowsEditing = YES;
- picker.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentViewController:picker animated:YES completion:^{}];
- }
- UIImagePickerController *picker = [[UIImagePickerController alloc]init];
- picker.delegate = self;
- picker.allowsEditing = YES;
- [self presentViewController:picker animated:YES completion:^{}];
选择照片代码
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- UIImageView *imageview = (UIImageView*)[self.view viewWithTag:100];
- imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];
- [picker dismissViewControllerAnimated:YES completion:^{}];
- NSLog(@"%@",info);
- }
代理方法
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
- [self dismissViewControllerAnimated:YES completion:^{}];
- }
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- UIImageView *imageview = (UIImageView*)[self.view viewWithTag:100];
- <div class="line number67 index66 alt2"><code class="cpp spaces"> </code><code class="cpp comments">// UIImagePickerControllerOriginalImage 原始图片</code></div><div class="line number68 index67 alt1"><code class="cpp spaces"> </code><code class="cpp comments">// UIImagePickerControllerEditedImage 编辑后图片</code></div>
- imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];
- [picker dismissViewControllerAnimated:YES completion:^{}];
- NSLog(@"%@",info);
- }
电话、短信、发邮件是手机的基础功能,iOS中提供了接口,让我们调用。这篇文章简单的介绍一下iOS的打电话、发短信在程序中怎么调用。
1、发短信
MFMessageComposeViewController提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.
messageComposeDelegate :代理,处理发送结果
recipients :收信人<列表,支持群发>
Frameworks中要引入MessageUI.framework
#import
添加协议:<MFMessageComposeViewCont
- // MFMessageComposeViewController *mf = [[MFMessageComposeViewController alloc]init];
- // if ([MFMessageComposeViewController canSendText]) {
- // mf.body = @"中午我回去不";
- // mf.recipients = [NSArray arrayWithObjects:@"13888888888", nil];
- // mf.messageComposeDelegate = self;
- // [self presentViewController:mf animated:YES completion:^{}];
- // }
必要的代理方法
- - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
- [self dismissViewControllerAnimated:YES completion:^{}];
- switch ( result ) {
- case MessageComposeResultCancelled:
- [self alertWithTitle:@"提示信息" msg:@"发送取消"];
- break;
- case MessageComposeResultFailed:// send failed
- [self alertWithTitle:@"提示信息" msg:@"发送失败"];
- break;
- case MessageComposeResultSent:
- [self alertWithTitle:@"提示信息" msg:@"发送成功"];
- break;
- default:
- break;
- }
- }
- - (void) alertWithTitle:(NSString *)title msg:(NSString *)msg {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
- message:msg
- delegate:self
- cancelButtonTitle:nil
- otherButtonTitles:@"确定", nil];
- [alert show];
- }
2、打电话
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10010"]];//打电话
使用openURL这个API打电话结束后,返回的是系统的拨打电话界面,如何才能返回自己的应用呢?有两种方法与大家分享。
第一种是用UIWebView加载电话,这种是合法的,可以上App Store的。
代码如下:
- UIWebView*callWebview =[[UIWebView alloc] init];
- NSURL *telURL =[NSURL URLWithString:@"tel:10010"];
- [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
- //记得添加到view上
- [self.view addSubview:callWebview];
第二种是私有方法,不能上App Store的(自己没试过)。
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"telprompt://10010"]];
上面的代码只是把第一个方法中的tel为telprompt.
3、发邮件
发送邮件代码
- MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];
- mail.mailComposeDelegate = self;
- [mail setMessageBody:@"jintiantianqibucuo" isHTML:YES];//邮箱内容
- [mail setToRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//发送对象
- [mail setCcRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//抄送人
- [mail setBccRecipients:[NSArray arrayWithObjects:@"1161913145@qq.com",nil]];//密送对象
- [mail setSubject:@"hello world"];//主题
- [self presentViewController:mail animated:YES completion:^{}];
-