我有一个相机应用程序,用户可以在其中分享通过电子邮件拍摄的照片。 我正在使用MFMailComposerViewController发送邮件。这是一段代码。
- (void) contactEmailRecepients:(NSArray *)emailIDs
subject:(NSString *)subject
attachment:(NSMutableDictionary *)attachmentDictionary
sender:(UIViewController *)sender
{
if ([MFMailComposeViewController canSendMail])
{
mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:emailIDs];
[mailViewController setSubject:subject];
NSData *attachmentData = [attachmentDictionary objectForKey:@"attachmentData"];
if (nil != attachmentData)
{
[mailViewController addAttachmentData:attachmentData
mimeType:[attachmentDictionary objectForKey:@"mimeType"]
fileName:[attachmentDictionary objectForKey:@"fileName"]];
}
[sender presentViewController:mailViewController
animated:YES
completion:^{}];
attachmentData = nil;
[attachmentDictionary removeAllObjects];
attachmentDictionary = nil;
}
else
{
// display error message
}
}
我的问题是,每次通过我的应用程序发送邮件时,VM(虚拟内存)都会增加6/7 MB。但如果我在以下部分发表评论,就不会发生这种情况。
if (nil != attachmentData)
{
[mailViewController addAttachmentData:attachmentData
mimeType:[attachmentDictionary objectForKey:@"mimeType"]
fileName:[attachmentDictionary objectForKey:@"fileName"]];
}
当我通过Xcode 5.0.2工具检查时,增加的VM归因于CGRasterData
且负责的库是CoreGraphics
并且负责的调用者是CGDataProvideCreateWithCopyOfData
。所以某个地方的副本正在获得创建后未发布。我怀疑分配给显示电子邮件UIActionSheet
中的图像的内存未被释放。
感谢任何帮助。
修改
添加attachmentData
初始化的代码段。
- (void)postPhotoFromPath:(NSString *)filePath
sender:(UIViewController *)sender
{
NSData *photoData = [NSData dataWithContentsOfFile:filePath];
if (nil == photoData)
{
//display error message
}
else
{
NSString *fileName = [[filePath componentsSeparatedByString:@"/"] lastObject];
[self contactEmailRecepients:nil
subject:nil
attachment:[NSDictionary dictionaryWithObjectsAndKeys:
photoData, @"attachmentData",
@"image/jpeg", @"mimeType",
fileName, @"fileName",
nil]
sender:sender];
}
}
我还注意到,当我得到UIActionSheet
关于要发送哪个图像尺寸(无论是发送原始尺寸还是缩小尺寸)时,VM会完全增加。我在这里附上相同的屏幕截图。