xCode图像操作内存泄漏

时间:2015-07-18 13:53:52

标签: ios xcode memory memory-leaks uiimage

Hello Stackoverflow社区,

我目前正在开发一个iOS项目的问题。它经历了一个过程,它必须从服务器下载图像,围绕它们并将它们放在黑色背景上,最后将它们保存为文件。这会遍历超过2.000个不同图像的URL,并且必须分别处理它们。问题是某处似乎存在巨大的内存泄漏,我无法弄清楚如何解决它。应用程序终止前会触发内存警告四次。我希望有人能够启发我。

func getRoundedPNGDataFromData(imageData: NSData) -> NSData? {
    let Image: UIImage? = UIImage(data: imageData)!
    let ImageFrame: CGRect = CGRectMake(0, 0, Image!.size.width, Image!.size.height)
    UIGraphicsBeginImageContextWithOptions(Image!.size, false, 1.0);
    let CurrentContext: CGContextRef = UIGraphicsGetCurrentContext()
    CGContextSetFillColorWithColor(CurrentContext, UIColor.blackColor().CGColor)
    CGContextFillRect(CurrentContext, ImageFrame)
    UIBezierPath(roundedRect: ImageFrame, cornerRadius: 10.0).addClip()
    Image!.drawInRect(ImageFrame)
    let NewImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return UIImagePNGRepresentation(NewImage!)
}

...
var ProgressCounter: Int = 0
    for OID: String in MissingThumbnails {
    let CurrentProgress: CFloat = (CFloat(ProgressCounter) / CFloat(MissingThumbnails.count))
    dispatch_sync(dispatch_get_main_queue(), {
        progressView?.progress
        progressView?.setProgress(CurrentProgress, animated: true)
    })
    let ThumbURL: NSURL = NSURL(string: "http://\(Host):\(Port)/\(self.WebAPIThumbnailPath)".stringByReplacingOccurrencesOfString("${OID}", withString: OID, options: [], range: nil).stringByReplacingOccurrencesOfString("${ThumbnailNumber}", withString: self.padID(5), options: [], range: nil))!
    var ThumbData: NSData? = NSData(contentsOfURL: ThumbURL)
    if (ThumbData != nil) {
        if (ThumbData!.length > 0) {
                var RoundedPNG: NSData? = self.getRoundedPNGDataFromData(ThumbData!)
            if ((RoundedPNG!.writeToFile(FSTools.getDocumentSub("\(self.Structs[Paths.OBJ_THUMBNAILS])/\(OID).png"), atomically: false)) == false) {
                dispatch_sync(dispatch_get_main_queue(), {() -> Void in
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                    delegate?.thumbnailCacheCompleted?(false)
                })
            }
            RoundedPNG = nil
            ThumbData = nil
        }
    }
    ProgressCounter++
}

任何有用的答案将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在循环内添加autoreleasepool以允许释放自动释放的内存。

$model->attachment = UploadedFile::getInstances($model, 'attachment');
    if ($model->attachment) {

      $value = Yii::$app->mailer->compose()
        ->setFrom([Yii::$app->user->identity->email => 'Sample Mail'])
        ->setTo($model->email)
        ->setSubject($model->subject)
        ->setHtmlBody($model->content);

      foreach ($model->attachment as $file) {
        $name = 'emailattachments/' . $file->baseName . '.' . $file->extension;
        $file->saveAs($name);
        $value->attach(\Swift_Attachment::fromPath($name));
      }

      $value->send();
    }

当剩下当前的autoreleasepool范围时,会释放自动释放的内存,这通常发生在runloop中。但是如果代码紧张并且runloop没有运行,则内存将不会及时释放。在每次迭代时添加显式自动释放池将允许在循环的每次迭代中回收此临时自动释放内存。