首先,我是新手,感谢您的耐心等待。
我正在尝试开发自己的照片马赛克程序——对我来说,这是一个学习之旅。
作为程序的一部分,我试图枚举图像文件夹,计算RGB
图像像素的平均值,将此数据存储在NSDictionary
对象中,然后收集NSDictionary
数组中每个图像的数据。
目前,当我尝试学习东西时,代码有点像打瞌睡的方法——而且我还没有内置任何错误捕获功能。
代码有效,但是,当我通过工具运行它时,我会为大量对象分配大量的资源,NSCalibratedRGBColor
这些对象似乎在例程结束时没有被释放。
欢迎任何有关如何处理此内存问题的想法/指导。
我正在使用以下代码来计算平均 RGB。
while (tempFile = [dirEnum nextObject]) {
//need to process the images within this while loop
if ([filesAllowed containsObject:[tempFile pathExtension]]) {
count = count + 1;
test = [NSString stringWithFormat:@"%@/%@", imageSourceFilePath, tempFile];
tempFileURL = [NSURL fileURLWithPath:test];
CIImage *sourceCIImage = [[CIImage alloc] initWithContentsOfURL:tempFileURL];
NSBitmapImageRep *tempBitmapImageRep = [[NSBitmapImageRep alloc] initWithCIImage:sourceCIImage];
NSInteger imageHeight = [tempBitmapImageRep pixelsHigh];
NSInteger imageWidth = [tempBitmapImageRep pixelsWide];
int totalPixels = (int)imageWidth * (int)imageHeight;
countPixelsWide = 0;
countPixelsHigh = 0;
totalGreen = 0;
totalBlue = 0;
totalRed = 0;
//work out averages - iterate over each column
while (countPixelsWide < imageWidth) {
while (countPixelsHigh < imageHeight) {
tempColor = [tempBitmapImageRep colorAtX:countPixelsWide y:countPixelsHigh];
[tempColor getRed:&tempRed green:&tempGreen blue:&tempBlue alpha:&tempAlpha];
totalRed = totalRed + tempRed;
totalBlue = totalBlue + tempBlue;
totalGreen = totalGreen + tempGreen;
countPixelsHigh = countPixelsHigh + 1;
}
countPixelsWide = countPixelsWide + 1;
countPixelsHigh = 0;
}
//calculate averages - re-use total variables
totalRed = totalRed/totalPixels;
totalBlue = totalBlue/totalPixels;
totalGreen = totalGreen/totalPixels;
//create dictionary for each source image and then add to sourceImageArray for later reference
dictOfSourceImages = [self makeDictionaryRecord:tempFileURL withAverageRed:[NSNumber numberWithFloat:totalRed] withAverageBlue:[NSNumber numberWithFloat:totalBlue] withAverageGreen:[NSNumber numberWithFloat:totalGreen]];
[sourceImageData addObject:dictOfSourceImages];
}
}