我创建了一个应用程序,其中用户使用前置摄像头拍照并通过电子邮件发送,并且该应用程序至少 8 小时内永远不会进入后台;该应用程序显示在办公室中,并且必须始终位于前台。一切都发生在UIViewController中。
问题出在MFMailComposeViewController上,特别是它消耗的内存。在仪器 - 活动监视器中,有一个MailCompositionS,它在实际内存使用量中不断增加。
我的代码是:
- (void)emailPhoto
{
NSString *emailTitle;
NSString *messageBody;
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
NSData *dataImage = UIImageJPEGRepresentation(photoView.image, 0.4);
[mc addAttachmentData:dataImage mimeType:@"image/jpeg" fileName:@"image.jpg"];
[self presentViewController:mc animated:YES completion:nil];
mc = nil;
dataImage = nil;
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:^{
[self backToCamera];
}];
}
我真的不明白可能是什么问题。无论是否带有图像附件,MailCompositionS 的内存消耗都会不断增加,并且由于应用程序始终保留在该 UIViewController 中并且永远不会进入后台,因此永远不会释放内存。
PS我没有检测到任何泄漏。
我认为您必须在属性中创建一次MFMailComposeViewController
实例。 而不是为每个照片创建一个实例。 只需复制几行。 self.mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
viewDidLoad()
或viewWillAppear()
.
祝你好运