我正在尝试使用 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]);
}
}