我对 Cocoa 开发很陌生,我可能不太清楚 ARC 是如何工作的。
我的问题是,当我使用 NSImageView 时,它没有按我的意愿被释放,所以程序正在泄漏内存。
__block CMTime lastTime = CMTimeMake(-1, 1);
__block int count = 0;
[_imageGenerator generateCGImagesAsynchronouslyForTimes:stops
completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime,
AVAssetImageGeneratorResult result, NSError *error)
{
if (result == AVAssetImageGeneratorSucceeded)
{
if (CMTimeCompare(actualTime, lastTime) != 0)
{
NSLog(@"new frame found");
lastTime = actualTime;
}
else
{
NSLog(@"skipping");
return;
}
// place the image onto the view
NSRect rect = CGRectMake((count+0.5) * 110, 100, 100, 100);
// the problem is here!!! ImageView object gets allocated, but never released by the program even though I'm using ARC
NSImageView *imgV = [[NSImageView alloc] initWithFrame:rect];
[imgV setImageScaling:NSScaleToFit];
NSImage *myImage = [[NSImage alloc] initWithCGImage:image size:(NSSize){50.0,50.0}];
[imgV setImage:myImage];
[self.window.contentView addSubview: imgV];
}
if (result == AVAssetImageGeneratorFailed)
{
NSLog(@"Failed with error: %@", [error localizedDescription]);
}
if (result == AVAssetImageGeneratorCancelled)
{
NSLog(@"Canceled");
}
count++;
}];
因此,当我再次返回此块以生成新图像并显示它们时,一切正常,除了我的程序内存使用量因创建的视图数量而增加。
如果有人可以帮助我,我将不胜感激!谢谢!