首页 > 学院 > 开发设计 > 正文

IOS中摇一摇实现截屏(可实现问题反馈的功能)

2019-11-14 20:40:23
字体:
来源:转载
供稿:网友

  有一段时间没有更新博客了,今天更新一篇关于最近工作中用到的一个功能,先简单描述一下:我们知道,测试人员在测试客户端产品时,当出现问题或者BUG的时候,都得先对页面截图,然后从相册中选择截图,加上一段描述放到TD库或者直接通过邮件发给开发人员,以方便开发人员进行修改,过程繁琐。通过下面功能的介绍,您可以很方便的让测试人员遇到BUG的时候,摇一摇设备,然后自动截图并保存到应用的沙盒之中。截图保存到沙盒之后,您可以自定义一个问题反馈的页面,对问题进行描述(描述可使用第三方平台,如讯飞语音,实现语音录入的效果)之后,将描述信息和页面截图一起发送给开发人员(可以是邮件,也可以是直接发送到后台)。

  下面来看看功能是怎么实现的。

  首先,定义了一个category:ShakeAndCutter

  UIViewController+ShakeAndCutter.h文件源码

#import <UIKit/UIKit.h>@interface UIViewController (ShakeAndCutter)@end

  UIViewController+ShakeAndCutter.m文件源码:

 1 #import "UIViewController+ShakeAndCutter.h" 2 #import <QuartzCore/QuartzCore.h> 3 #import "TestFeedbackViewController.h" 4  5 @implementation UIViewController (ShakeAndCutter) 6  7 - (BOOL)canBecomeFirstResponder 8 { 9     return YES;10 }11 12 #PRagma mark - 摇一摇动作处理13 14 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event15 {16     NSLog(@"began");17 }18 19 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event20 {21     NSLog(@"cancel");22 }23 24 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event25 {26     NSLog(@"end");27     [self cutterViewToDocument];28     29     // 这里是自定义的问题反馈页面,也可以直接跳转到系统邮件发送的页面30     TestFeedbackViewController *testFeedbackViewController = [[[TestFeedbackViewController alloc] initWithNibName:@"TestFeedbackViewController" bundle:nil] autorelease];31     [self.navigationController pushViewController:testFeedbackViewController animated:YES];32 }33 34 #pragma mark - 截屏处理35 36 - (void)cutterViewToDocument37 {38     UIWindow *screenWindow = [[UIapplication sharedApplication] keyWindow];39     40     UIGraphicsBeginImageContext(screenWindow.frame.size);41     [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];42     UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();43     UIGraphicsEndImageContext();44     45     NSData *screenShotPNG = UIImagePNGRepresentation(screenShot);46     NSError *error = nil;47     [screenShotPNG writeToFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"error.png"] options:NSAtomicWrite error:&error];48 }49 50 @end

  至此,摇一摇和截图功能就已经完成了,那么在应用中怎么使用呢?

  • 对于IOS6.0以上的设备,只需要在项目的pch文件中引进头文件#import "UIViewController+ShakeAndCutter.h"即可(我在IOS6.0.1IOS6.1.5IOS7.0.6设备上已经测试过),这样测试工作完成之后,您只需要将这行代码注释或者删除就可以了;
  • 对于IOS6.0以下的设备,在需要实现摇一摇效果的页面,有可能要加入如下代码(手头没有IOS6.0以下的设备,如果大家有的话,帮忙验证一下,非常感谢):

  1.viewDidLoad方法中,添加两行代码:

1 [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];2 [self becomeFirstResponder];

        2.viewDidAppear方法中,添加一行代码:

1 [self becomeFirstResponder];

        3.viewDidDisappear方法中,添加一行代码:

1 [self resignFirstResponder];

  当然,以上的功能,您也可以实现其他的效果:比如说用户问题的反馈等等,具体可以根据应用的需求而定。

  

  这里,再附上发送到邮件功能的示例代码:

  MailViewController.h文件源码:

 1 #import <UIKit/UIKit.h> 2 #import <MessageUI/MessageUI.h> 3 #import<MessageUI/MFMailComposeViewController.h> 4  5 @interface MailViewController : UIViewController<MFMailComposeViewControllerDelegate, 6 MFMessageComposeViewControllerDelegate> 7 { 8     UIButton                    *shareToMailButton; 9 }10 11 @end

  MailViewController.m文件源码:

  1 #import "MailViewController.h"  2 #import "Constants.h"  3   4 @interface MailViewController ()  5   6 @end  7   8 @implementation MailViewController  9  10 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 11 { 12     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 13     if (self) { 14         // Custom initialization 15     } 16     return self; 17 } 18  19 - (void)viewDidLoad 20 { 21     [super viewDidLoad]; 22     // Do any additional setup after loading the view. 23      24     self.title = @"Bug反馈"; 25      26     shareToMailButton = [self buttonWithFrame:CGRectMake(20, 100, 280, 40) action:@selector(btnClicked:) withTag:1000]; 27     [shareToMailButton setTitle:@"Bug反馈" forState:UIControlStateNormal]; 28 } 29  30 - (void)didReceiveMemoryWarning 31 { 32     [super didReceiveMemoryWarning]; 33     // Dispose of any resources that can be recreated. 34 } 35  36 #pragma mark - 自定义方法 37  38 - (void)btnClicked:(id)sender 39 { 40     UIButton *btnSender = (UIButton *)sender; 41      42     switch (btnSender.tag) 43     { 44         case 1000:  // 分享到邮箱 45         { 46             [self showMailPicker]; 47             break; 48         } 49         default: 50             break; 51     } 52 } 53  54 /******************************************************************************* 55  * 方法名称:buttonWithFrame:action: 56  * 功能描述:初始化页面按钮,并添加到页面视图 57  * 输入参数: 58  * 输出参数: 59  ******************************************************************************/ 60 - (UIButton *)buttonWithFrame:(CGRect)frame action:(SEL)action withTag:(int)tag 61 { 62     UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; 63     UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5]; 64      65     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 66     button.frame = frame; 67     [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal]; 68     [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled]; 69     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 70     [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 71     [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 72     button.tag = tag; 73     [self.view addSubview:button]; 74      75     return button; 76 } 77  78 - (void)showMailPicker 79 { 80     Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 81      82     if (mailClass !=nil) 83     { 84         if ([mailClass canSendMail]) 85         { 86             [self displayMailComposerSheet]; 87         } 88         else 89         { 90             UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@""message:@"设备不支持邮件功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; 91             [alert show]; 92             [alert release]; 93         } 94     } 95     else 96     { 97          98     } 99     100 }101 102 - (void)displayMailComposerSheet103 {104     MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];105     106     // 设置picker的委托方法,完成之后会自动调用成功或失败的方法107     picker.mailComposeDelegate = self;108     // 添加主题109     [picker setSubject:@"Bug反馈"];110     // 添加收件人111     NSArray *toRecipients = [NSArray arrayWithObject:@"279352257@QQ.com"];112     // 说明:也可以添加多个收件人,代码如下所示:113 //    NSArray *toRecipients = [NSArray arrayWithObjects:@"one@qq.com",@"two@qq.com",nil];114     // 添加抄送115 //    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];116     // 添加密送117 //    NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];118     119     [picker setToRecipients:toRecipients];120 //    [picker setCcRecipients:ccRecipients];121 //    [picker setBccRecipients:bccRecipients];122     // 发送图片附件(其他格式的附件,可以都先转化称NSData类型,然后设置相应的mimeType即可,如txt类型为@"text/txt",doc类型为@"text/doc",pdf类型为@"file/pdf"等等)123     NSData *myData = [NSData dataWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"error.png"]];124     [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"error.png"];125     NSString *emailBody = [NSString stringWithFormat:@"<p>文字没有全部显示</p>"];126     127     // 直接在HTML代码中写入图片的地址128 //    NSString *emailBody = [NSString stringWithFormat:@"<img src='http://p2.so.qhimg.com/t0130e3288d86929b97.jpg' /><p>我分享了图片</p>"];129     130     [picker setMessageBody:emailBody isHTML:YES];131     [self presentModalViewController:picker animated:YES];132     [picker release];133 }134 135 136 - (void)mailComposeController:(MFMailComposeViewController*)controller137           didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error138 {139     switch (result)140     {141         case MFMailComposeResultCancelled:142             NSLog(@"Result: Mail sending canceled");  // 邮件发送取消  143             break;144         case MFMailComposeResultSaved:145             NSLog(@"Result: Mail saved");  // 邮件保存成功146             break;147         case MFMailComposeResultSent:148             NSLog(@"Result: Mail sent");  // 邮件发送成功149             break;150         case MFMailComposeResultFailed:151             NSLog(@"Result: Mail sending failed");  // 邮件发送失败152             break;153         default:154             NSLog(@"Result: Mail not sent");155             break;156     }157     [self dismissModalViewControllerAnimated:YES];158 }159 160 - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result161 {162     NSLog(@"messageComposeViewController");163 }164 165 @end

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表