CGImageRef grayscaleImage;
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapCtx = CGBitmapContextCreate(bwbuffer, width, height, 8, width, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
grayscaleImage = CGBitmapContextCreateImage(bitmapCtx);
CFRelease(bitmapCtx);
CFRelease(colorSpace);
}
UIImage *temp = [[UIImage alloc] initWithCGImage:grayscaleImage];
CFStringRef UTI= (CFStringRef)@"public.heic";
NSMutableData *newImageData = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)newImageData,UTI,1,NULL);
CGImageDestinationAddImage(destination, temp.CGImage, (__bridge CFDictionaryRef)metaDataDictNew);
CGImageDestinationFinalize(destination);
This code running on iOS15. However iOS16 can't write grayscale HEIC image file.RGB image file is OK. The following were error message on console of xcode.
writeImageAtIndex:867: *** CMPhotoCompressionSessionAddImage: err = kCMPhotoError_InvalidData [-17102] (codec: 'hvc1')
I can't find this error code in the Apple Support site also Documentation. Best regards.