iphone - 在 MFMailComposeViewController 中附加多张照片的内存泄漏

标签 iphone ios multithreading cgimage alasset

我正在尝试在 MailComposerViewController 中附加多张照片,并且我正在使用 ALAssetPickerViewController 来选择多张照片。我有一个 NSMutableArray,其中包含所选 Assets 的引用。我正在实现一个 for 循环,它枚举所选 Assets 的 array 以获取 UIImageNSData >UIImage 使用 CGImageRef 初始化。 代码如下:

@autoreleasepool
{
    NSString *emailTitle = @"Test";
    NSString *messageBody = @"IOS programming is so fun!";
    NSArray *toRecipents = [NSArray arrayWithObjects:@"abc@gmail.com", nil];

    MFMailComposeViewController *tempmcvc = nil;
    tempmcvc = [[MFMailComposeViewController alloc] init];
    tempmcvc.mailComposeDelegate = self;
    [tempmcvc setSubject:emailTitle];
    [tempmcvc setMessageBody:messageBody isHTML:YES];
    [tempmcvc setToRecipients:toRecipents];
    tempmcvc.modalPresentationStyle=UIModalPresentationFullScreen;
    tempmcvc.navigationBar.barStyle = UIBarStyleBlackOpaque;

    for (AlAsset *assets in SelectedAssetsarray) 
    {
        @autoreleasepool
        {
            UIImage *attachImagTemp = nil;
            NSData *myData = nil;
            CGImageRef iref = [assets.defaultRepresentation fullScreenImage];
            NSString *nameOfImgTemp;
            attachImagTemp = [UIImage imageWithCGImage:iref];
            nameOfImgTemp = assets2.defaultRepresentation.filename;                      
            myData = UIImageJPEGRepresentation (attachImagTemp, 1.0);
            [tempmcvc addAttachmentData:myData mimeType:@"image/jpeg" fileName:nameOfImgTemp];

            myData = nil;
            attachImagTemp = nil;
            iref = nil;
            nameOfImgTemp = nil;
            ALAsset *_temp = assets2;
            _temp = nil;

        }
    }
}

dispatch_async(dispatch_get_main_queue(), ^(void) {
    [self presentModalViewController:tempmcvc animated:YES]
});

我附加的每项 Assets 几乎都是 2 MB,但内存不断减少我无法正确释放内存;一些内存泄漏请帮助找到泄漏。

最佳答案

尝试使用以下代码释放附件图像 CGImageRelease(attachImagTemp); 以释放缓存内存而不是将其设置为 nil。

如果您还没有使用 ARC,请按照 Avi 的建议释放 tempmcvc 对象。

关于iphone - 在 MFMailComposeViewController 中附加多张照片的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161740/

相关文章:

iphone - 无法从 iOS 中的框架访问 .nib(XIB) 文件

iphone - 处理何时回调已释放的委托(delegate)?

Python+Tkinter,如何在独立于tk前端的后台运行?

c++ - 在这种情况下有什么正确的方法来实现锁定?

ios - UIView 未从 super View 中删除

iphone - 如何在 MKMapView 上绘制当前位置到所需位置之间的路线?

ios - 如何在 iOS 中将 UILabel 的字体名称设置为 HelveticaNeue Thin?

iphone - 选择不支持iOS设备

ios - 从应用程序内部发送用户赠送应用程序不再有效?

C:从后台线程返回异步错误的模式?