应用过滤器时释放CGDataProviderWithCopyOfData

时间:2014-10-20 16:06:59

标签: xcode ios7 ios7.1

我有一个应用色调效果过滤器并创建CGBitmapContext的方法。每当用户点击过滤器的按钮时,它就会增加内存并且ARC不会释放内存。每次增加600KB。我做了一些研究,发现仪器分配,Core Graphics在 CGDataProviderWithCopyOfData 上增加了600KB。我该如何发布 CGRaster数据

以下是方法:

-(UIImage *)modifyImageTintColor:(UIImage *)source imageRawData:(Byte *)rawData degree:(int)degree{

double PI = 3.14159;
double HALF_CIRCLE_DEGREE = 180;
double RANGE = 256;

int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;
double angle = (PI * (double)degree) / HALF_CIRCLE_DEGREE;

int S = (int)(RANGE * sin(angle));
int C = (int)(RANGE * cos(angle));

CGFloat r, g, b;
//apply sepia effect

int byteIndex = 0;
for(int i = 0; i < _imageWidth * _imageHeight; ++i){

    r = rawData[byteIndex];
    g = rawData[byteIndex + 1];
    b = rawData[byteIndex + 2];

    RY = ( 70 * r - 59 * g - 11 * b ) / 100;
    GY = (-30 * r + 41 * g - 11 * b ) / 100;
    BY = (-30 * r - 59 * g + 89 * b ) / 100;
    Y  = ( 30 * r + 59 * g + 11 * b ) / 100;
    RYY = ( S * BY + C * RY ) / 256;
    BYY = ( C * BY - S * RY ) / 256;
    GYY = (-51 * RYY - 19 * BYY ) / 100;
    R = Y + RYY;
    R = ( R < 0 ) ? 0 : (( R > 255 ) ? 255 : R );
    G = Y + GYY;
    G = ( G < 0 ) ? 0 : (( G > 255 ) ? 255 : G );
    B = Y + BYY;
    B = ( B < 0 ) ? 0 : (( B > 255 ) ? 255 : B );

    rawData[byteIndex] = R;
    rawData[byteIndex + 1] = G;
    rawData[byteIndex + 2] = B;

    byteIndex += 4;


}

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * _imageWidth;
NSUInteger bitsPerComponent = 8;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef;

CGContextRef ctx;
ctx = CGBitmapContextCreate(rawData,
                            _imageWidth,
                            _imageHeight,
                            bitsPerComponent,
                            bytesPerRow,
                            colorSpace,
                            kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

imageRef = CGBitmapContextCreateImage(ctx);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
CFRelease(ctx);

return result;

}

1 个答案:

答案 0 :(得分:0)

您需要在CFRelease上拨打imageRef,如下所示:

imageRef = CGBitmapContextCreateImage(ctx);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CFRelease (imageRef);
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
CFRelease(ctx);

此外,它总是使用CFRelease而不是像这样的自定义发布器,看起来更整洁(并且更具可读性):

imageRef = CGBitmapContextCreateImage(ctx);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CFRelease (imageRef);
CGRelease(ctx);
CGRelease(colorSpace);
CFRelease(ctx);