まず、私は初心者ですので、ご理解のほどよろしくお願いいたします。
私は自分のフォト モザイク プログラムを開発しようとしています。
プログラムの一部として、画像のフォルダーを列挙し、画像ピクセルの平均を計算し、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];
}
}