6

我一直在寻找解决以下问题的解决方案,但找不到。我做了过滤方法来修改HSV. UIImage此方法工作正常,但是当我使用此方法时,使用的内存会增长并且无法释放。在 Instruments 的“Live Bytes”框中,我发现CFData(存储)的值每次过滤都会增加几 MB。我怎样才能释放它们?我发现了类似的问题,但对于这个问题并不好。谢谢你。

//Objective-C
- (UIImage*)addFilter:(UIImage*)img{
    CGImageRef imgRef = img.CGImage;

    //getting size
    size_t width = CGImageGetWidth(imgRef);
    size_t height = CGImageGetHeight(imgRef);
    size_t bitsPerComponent = CGImageGetBitsPerComponent(imgRef);
    size_t bitsPerPixel = CGImageGetBitsPerPixel(imgRef);
    size_t bytesPerRow = CGImageGetBytesPerRow(imgRef);


    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imgRef);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgRef);
    bool shouldInterpolate = CGImageGetShouldInterpolate(imgRef);
    CGColorRenderingIntent renderingIntent = CGImageGetRenderingIntent(imgRef);
    CGDataProviderRef dataProvider = CGImageGetDataProvider(imgRef);
    CFMutableDataRef *data = CGDataProviderCopyData(dataProvider);
    UInt8 *buf = (UInt8 *)CFDataGetMutableBytePtr(data);


    double h,s,v;
    double c;
    double cmax,cmin;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {

        /////////////////////
        ///change HSV value//
        /////////////////////

        }
    }

    CFDataRef  filterData = CFDataCreate(NULL, buf, CFDataGetLength(data));
    CGDataProviderRef filterDataProvider = CGDataProviderCreateWithCFData(filterData);
    CGImageRef filterCgImage = CGImageCreate(
                                             width,
                                             height,
                                             bitsPerComponent,
                                             bitsPerPixel,
                                             bytesPerRow,
                                             colorSpace,
                                             bitmapInfo,
                                             filterDataProvider,
                                             NULL,
                                             shouldInterpolate,
                                             renderingIntent
                                             );

    UIImage *imgFinal =  [UIImage imageWithCGImage:filterCgImage];

    CFRelease(filterData);
    CGImageRelease(filterCgImage);
    CGDataProviderRelease(filterDataProvider);
    CFRelease(data);

    return imgFinal;

}
4

0 回答 0