0

我正在尝试使用 AVCaptureSession 获取图像。我遵循了本教程http://www.benjaminloulier.com/posts/2-ios4-and-direct-access-to-the-camera。我正在从图像参考创建 uiimage,然后从该 uiimage 获取像素。但应用程序在一段时间后(不到 30 秒)崩溃。我尝试使用 Leaks 进行分析,但它也崩溃了。使用日志我发现应用程序在 CGContextDrawImage(context, rect, image1.CGImage); 行之前崩溃。你们对我可能做错的事情有什么建议吗?我还在应用程序崩溃前几秒钟看到内存分配错误。请帮忙。

代码贴在下面..

// Create a UIImage from sample buffer data

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 
{
lock = @"YES";

 CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 

// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0); 

// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer); 
size_t height = CVPixelBufferGetHeight(imageBuffer); 

// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 


size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);

// Create a Quartz direct-access data provider that uses data we supply.
NSData *data = [NSData dataWithBytes:baseAddress length:bufferSize];

CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

CGImageRef quartzImage = CGImageCreate(width, height, 8, 32, bytesPerRow,
              colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
              dataProvider, NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

// Unlock the pixel buffer

CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];

// Release the Quartz image
CGImageRelease(quartzImage);

CVPixelBufferUnlockBaseAddress(imageBuffer,0);
baseAddress = nil;
[data  release];
lock = @"NO";
return(image);
}

-(void)calculate
{
@try {

        UIImage *image1 = [self stillImage];   //Capture an image from the camera.
        //Extract the pixels from the camera image.

        CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();

        size_t bytesPerRow = image1.size.width*4;
        unsigned char* bitmapData = (unsigned char*)malloc(bytesPerRow*image1.size.height);

        CGContextRef context = CGBitmapContextCreate(bitmapData, image1.size.width, image1.size.height, 8, bytesPerRow,colourSpace,kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Big);

        CGColorSpaceRelease(colourSpace);

        CGContextDrawImage(context, rect, image1.CGImage);

        unsigned char* pixels = (unsigned char*)CGBitmapContextGetData(context);

        totalLuminance = 0.0;
        for(int p=0; p<image1.size.width*image1.size.height*4; p+=4)
        {
            totalLuminance += pixels[p]*0.3 + pixels[p+1]*0.59 + pixels[p+2]*0.11;
        }

        totalLuminance /= (image1.size.height * image1.size.width);                   

        pixels = nil;

        bitmapData = nil;

        [image1 release];

    CGContextRelease(context);
        //image1 = nil;

        //totalLuminance = [n floatValue];                   //Calculate the total luminance.
        float f = [del.camcont.slider value];
        float total = totalLuminance * f;
        NSString *ns = [NSString stringWithFormat:@"Lux : %0.2f", total];
        NSLog(@"slider = %f",f);
        NSLog(@"totlaluminance = %f",totalLuminance);
        NSLog(@"%@",ns);
        //NSString *ns = [NSString initWithFormat:@"Lux : %0.2f", total];
        [del.camcont.lux setText:ns];//Display the total luminance.

        self.stillImage = nil;
        //[self.stillImage release];
         ns = nil;
        //n = nil;
        //del = nil;
    }

    @catch (NSException *exception) {
        NSLog(@"main: Caught %@: %@", [exception name], [exception reason]);
}
}
4

2 回答 2

1

我不清楚你为什么要先创建 a 然后CMSampleBufferRef再创建 a然后CGImageRefUIImage取出数据然后将其推入指针中(本质上,指针指向的字节与首先)。UIImageCGImageRefunsigned charCMSampleBufferRef

如果您执行以下操作,您将简化您的生活(并且您应该会发现它更容易调试):

CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
size_t width = CVPixelBufferGetWidth(imageBuffer); 
size_t height = CVPixelBufferGetHeight(imageBuffer); 
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
uint8_t *pixels = malloc(bytesPerRow*height);
memcpy(pixels, baseAddress, bytesPerRow*height);
baseAddress = nil;
imageBuffer = nil;
sampleBuffer = nil;
float totalLuminance = 0.0;
for(int r=0; r<height; r++)
{
   for(int p=0, p<width, p+=4)
   {
      totalLuminance += pixels[p+(r*bytesPerRow)]*0.3 
                     + pixels[p+1+(r*bytesPerRow)]*0.59 
                     + pixels[p+2+(r*bytesPerRow)]*0.11;
   {
}
free(pixels);
totalLuminance /= (width * height);

(嵌套for循环是为了补偿由于填充而bytesPerRow不能假定与 相同的事实。)width*4

于 2012-09-02T16:21:27.203 回答
0

内存管理乍一看还不错。作为一种解决方法,您可以考虑 UIImage -imageWithData: 以防自定义 CGImageCreate 代码出现问题。这是因为无论如何您都在使用 CGImage 创建 UIImage 。

于 2012-08-31T15:33:43.050 回答