diff --git a/SDWebImageAVIFCoder/Classes/ColorSpace.m b/SDWebImageAVIFCoder/Classes/ColorSpace.m index 6a61f34..f04163f 100644 --- a/SDWebImageAVIFCoder/Classes/ColorSpace.m +++ b/SDWebImageAVIFCoder/Classes/ColorSpace.m @@ -215,7 +215,7 @@ void SDAVIFCalcColorSpaceMono(avifImage * avif, CGColorSpaceRef* ref, BOOL* shou } if(avif->icc.data && avif->icc.size) { if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) { - CFDataRef iccData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, avif->icc.data, avif->icc.size,kCFAllocatorNull); + CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size); *ref = CGColorSpaceCreateWithICCData(iccData); CFRelease(iccData); *shouldRelease = TRUE; @@ -313,7 +313,7 @@ void SDAVIFCalcColorSpaceRGB(avifImage * avif, CGColorSpaceRef* ref, BOOL* shoul } if(avif->icc.data && avif->icc.size) { if(@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) { - CFDataRef iccData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, avif->icc.data, avif->icc.size,kCFAllocatorNull); + CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, avif->icc.data, avif->icc.size); *ref = CGColorSpaceCreateWithICCData(iccData); CFRelease(iccData); *shouldRelease = TRUE; diff --git a/SDWebImageAVIFCoder/Classes/Conversion.m b/SDWebImageAVIFCoder/Classes/Conversion.m index 980a008..43e63e7 100644 --- a/SDWebImageAVIFCoder/Classes/Conversion.m +++ b/SDWebImageAVIFCoder/Classes/Conversion.m @@ -704,11 +704,11 @@ static CGImageRef CreateCGImage8(avifImage * avif) { } end_all: - free(resultBufferData); - free(argbBufferData); - free(dummyCbData); - free(dummyCrData); - free(scaledAlphaBufferData); + if (resultBufferData) free(resultBufferData); + if (argbBufferData) free(argbBufferData); + if (dummyCbData) free(dummyCbData); + if (dummyCrData) free(dummyCrData); + if (scaledAlphaBufferData) free(scaledAlphaBufferData); return result; } diff --git a/SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m b/SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m index c5adcbc..d881b7e 100644 --- a/SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m +++ b/SDWebImageAVIFCoder/Classes/SDImageAVIFCoder.m @@ -210,12 +210,14 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm vImage_Buffer src; v_error = vImageBuffer_InitWithCGImage(&src, &srcFormat, NULL, imageRef, kvImageNoFlags); if (v_error != kvImageNoError) { + vImageConverter_Release(convertor); return nil; } vImage_Buffer dest; - vImageBuffer_Init(&dest, height, width, hasAlpha ? 32 : 24, kvImageNoFlags); - if (!dest.data) { - free(src.data); + v_error = vImageBuffer_Init(&dest, height, width, hasAlpha ? 32 : 24, kvImageNoFlags); + if (v_error != kvImageNoError) { + if (src.data) free(src.data); + vImageConverter_Release(convertor); return nil; } @@ -224,7 +226,7 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm free(src.data); vImageConverter_Release(convertor); if (v_error != kvImageNoError) { - free(dest.data); + if(dest.data) free(dest.data); return nil; } @@ -232,7 +234,7 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm avifImage *avif = avifImageCreate((int)width, (int)height, 8, avifFormat); if (!avif) { - free(dest.data); + if (dest.data) free(dest.data); return nil; } avifRGBImage rgb = { @@ -245,7 +247,6 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm }; avifImageRGBToYUV(avif, &rgb); free(dest.data); - dest.data = NULL; NSData *iccProfile = (__bridge_transfer NSData *)CGColorSpaceCopyICCProfile([SDImageCoderHelper colorSpaceGetDeviceRGB]); @@ -264,14 +265,15 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm encoder->maxThreads = 2; avifResult result = avifEncoderWrite(encoder, avif, &raw); + avifImageDestroy(avif); + avifEncoderDestroy(encoder); if (result != AVIF_RESULT_OK) { - avifEncoderDestroy(encoder); + if (raw.data) avifRWDataFree(&raw); return nil; } NSData *imageData = [NSData dataWithBytes:raw.data length:raw.size]; - free(raw.data); - avifEncoderDestroy(encoder); + avifRWDataFree(&raw); return imageData; }