@@ -128,8 +128,10 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU
128128 .size = data.length
129129 };
130130 avifImage * avif = avifImageCreateEmpty ();
131- avifResult result = avifImageRead (avif, &rawData);
131+ avifDecoder *decoder = avifDecoderCreate ();
132+ avifResult result = avifDecoderRead (decoder, avif, &rawData);
132133 if (result != AVIF_RESULT_OK) {
134+ avifDecoderDestroy (decoder);
133135 avifImageDestroy (avif);
134136 return nil ;
135137 }
@@ -147,6 +149,7 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU
147149
148150 uint8_t * dest = calloc (width * components * height, sizeof (uint8_t ));
149151 if (!dest) {
152+ avifDecoderDestroy (decoder);
150153 avifImageDestroy (avif);
151154 return nil ;
152155 }
@@ -162,6 +165,7 @@ - (nullable CGImageRef)sd_createAVIFImageWithData:(nonnull NSData *)data CF_RETU
162165
163166 // clean up
164167 CGDataProviderRelease (provider);
168+ avifDecoderDestroy (decoder);
165169 avifImageDestroy (avif);
166170
167171 return imageRef;
@@ -274,17 +278,22 @@ - (nullable NSData *)encodedDataWithImage:(nullable UIImage *)image format:(SDIm
274278 if (options[SDImageCoderEncodeCompressionQuality]) {
275279 compressionQuality = [options[SDImageCoderEncodeCompressionQuality] doubleValue ];
276280 }
277- int rescaledQuality = 63 - (int )((compressionQuality) * 63 . 0f );
281+ int rescaledQuality = AVIF_WORST_QUALITY - (int )((compressionQuality) * AVIF_WORST_QUALITY );
278282
279283 avifRawData raw = AVIF_RAW_DATA_EMPTY;
280- avifResult result = avifImageWrite (avif, &raw, 2 , rescaledQuality);
284+ avifEncoder *encoder = avifEncoderCreate ();
285+ encoder->quality = rescaledQuality;
286+ encoder->maxThreads = 2 ;
287+ avifResult result = avifEncoderWrite (encoder, avif, &raw);
281288
282289 if (result != AVIF_RESULT_OK) {
290+ avifEncoderDestroy (encoder);
283291 return nil ;
284292 }
285293
286294 NSData *imageData = [NSData dataWithBytes: raw.data length: raw.size];
287295 free (raw.data );
296+ avifEncoderDestroy (encoder);
288297
289298 return imageData;
290299}
0 commit comments