1.UIImagePickerController 相片调用控制器
在进行app开发的时候,有的时候会通过设备获取 相片,那么就需要调用UIImagePickerController 这个控制器。
我们只需要简单的alloc出这个控制器实例,然后实现其对应的代理方法UIImagePickerControllerDelegate即可。
1)下面给出页面跳转的代码:
- (IBAction)selectAndMessageImage:(id)sender {
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
//首先设置当前的image获取源是 Camera。
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//如果相机不可用,那么就设置当前的imageSource为 相册。
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:nil];
}
2)实现代理方法 UIimagePickerControllerDelegate
#pragma mark UIImagePickerController Delegate Methods
//点击CHoose 按钮之后,表示已经选取了一张照片。那么imagePickerVC就消失,并调用showActivityViewController 方法。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self performSelector:@selector(showActivityViewController) withObject:nil afterDelay:1];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
3)下面的方法就是在选择之后调用的ActivityVC方法
- (void)showActivityViewController
{
NSString *message = @"i took a pickture on my iphone";
NSArray *activityItems = @[message,self.image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
2.短信 MFMessageComposeViewController
messageVC里面有两个选择项:recipients 是发送给谁,body是发送的消息内容。
- (IBAction)MessageVCShow:(id)sender {
MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc]init];
messageVC.messageComposeDelegate = self;
messageVC.recipients = @[@"xuna"];
messageVC.body = @"hello meimei";
[self presentViewController:messageVC animated:YES completion:nil];
}
//对我们发送的消息状态进行判断,并dismissVC
#pragma mark MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(@"cancel");
break;
case MessageComposeResultSent:
NSLog(@"sent");
break;
case MessageComposeResultFailed:
NSLog(@"faild");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
3.邮件 MFMailComposeViewController
邮件VC和messageVC 的区别就在于 里面的设置项的不同,其余都是相似的。
- (IBAction)mailVCShow:(id)sender {
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc]init];
mailVC.mailComposeDelegate = self;
[mailVC setToRecipients:@[@"10915819@qq.com"]];
[mailVC setCcRecipients:@[@"276560266@qq.com"]];
[mailVC setBccRecipients:@[@"1414129720@qq.com"]];
[mailVC setMessageBody:@"hello world" isHTML:NO];
NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];
NSData *data = [NSData dataWithContentsOfFile:path];
[mailVC addAttachmentData:data mimeType:@"image/png" fileName:@"surprisePic"];
[self presentViewController:mailVC animated:YES completion:nil];
}
//MFMailconposeVIewCOntrollerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"QUXIAO");
break;
case MFMailComposeResultFailed:
NSLog(@"FAILED");
break;
case MFMailComposeResultSaved:
NSLog(@"SAVE");
break;
case MFMailComposeResultSent:
NSLog(@"SENT");
break;
default:
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}