Xcode [noob] 释放对象,因为内存和 CPU 负载

Posted

技术标签:

【中文标题】Xcode [noob] 释放对象,因为内存和 CPU 负载【英文标题】:Xcode [noob] Release of objects, because of memory and CPU load 【发布时间】:2015-01-10 09:46:35 【问题描述】:

作为一名 c# unity 和 php 程序员,我完全是 Objective-c 的菜鸟。我设法将一个项目与cordoba-phonegap-objective C一起复制粘贴,以制作一个项目,其基本概念是一个类似于应用程序的镜像(具有各种额外的,仍需编程)..镜像工作,但在xcode中cpu和内存的负载不断增加,最后崩溃..当我搜索时,我认为我犯了一个错误,没有释放对象,但这是一个幸运的猜测..我希望你能帮助我......

#import "CanvasCamera.h"
@implementation CanvasCamera
- (void)startCapture:(CDVInvokedUrlCommand*)command

self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPreset352x288;
self.device = [self frontCamera];
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

self.output = [[AVCaptureVideoDataOutput alloc] init];
self.output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];

dispatch_queue_t queue;
queue = dispatch_queue_create("canvas_camera_queue", NULL);
[self.output setSampleBufferDelegate:(id)self queue:queue];
[self.session addInput:self.input];
[self.session addOutput:self.output];
[self.session startRunning];


- (AVCaptureDevice *)frontCamera 
 NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
    if ([device position] == AVCaptureDevicePositionFront) 
        return device;
    

return nil;



- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationUp];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *encodedString = [imageData base64Encoding];

NSString *javascript = @"CanvasCamera.capture('data:image/jpeg;base64,";
javascript = [javascript stringByAppendingString:encodedString];
javascript = [javascript stringByAppendingString:@"');"];
[self.webView performSelectorOnMainThread:@selector(stringByEvaluatingJavaScriptFromString:) withObject:javascript waitUntilDone:YES];

CGImageRelease(newImage);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);

@end

【问题讨论】:

【参考方案1】:

查看您对 NSAutoreleasepool 的使用,查看 Objective C 的自动引用计数和this documentation from Apple。

【讨论】:

我不明白整个 NSAutoreleasepool 的概念...我尝试了几件事 ..但它对我来说仍然是 abracadabra...你能帮我解释一下我的代码吗?跨度>

以上是关于Xcode [noob] 释放对象,因为内存和 CPU 负载的主要内容,如果未能解决你的问题,请参考以下文章

Xcode 中的内存管理

一个简单的 Objective C xcode 内存管理问题

如何使用 Xcode 检测对象的双重释放?

[C++11新特性] 智能指针详解

Objective C - 内存管理和自动释放???

oc56--ARC多个对象的内存管理