valid replacement for kUTTypeJPEG which is deprecated

I have the following code:

let ciImage = filterAndRender(ciImage: inputImage, doCrop: true)

        let outCGImage = ciContext.createCGImage(ciImage, from: ciImage.extent)!

        let dest = CGImageDestinationCreateWithURL(output.renderedContentURL as CFURL, kUTTypeJPEG, 1, nil)!

        CGImageDestinationAddImage(dest, outCGImage, [kCGImageDestinationLossyCompressionQuality as String:1] as CFDictionary)

        CGImageDestinationFinalize(dest)

I get the following caution: " 'kUTTypeJPEG' was deprecated in iOS 15.0: Use UTTypeJPEG instead."

However, when I substitute 'UTTypeJPEG' as directed, I get this error: "Cannot find 'UTTypeJPEG' in scope"

What should I use for kUTTypeJPEG instead?

Thanks!

Post not yet marked as solved Up vote post of Eric.app Down vote post of Eric.app
8.8k views

Replies

Seems the deprecation message is meant for Objective-C code. (You can send a bug report to Apple using Feedback Assistant.)

In Swift, Uniform Type Identifiers are wrapped into static members of UTType.

System Declared Uniform Type Identifiers

Can you try UTType.jpeg.identifier as CFString instead?

(You may need import UniformTypeIdentifiers if your source code does not have it yet.)

The same here for :

        kUTTypeFileURL 

        kUTTypeUTF8PlainText

despite import :

import MobileCoreServices
import UniformTypeIdentifiers

the error :

'kUTTypeFileURL' was deprecated in iOS 15.0: Use UTTypeFileURL instead.

'kUTTypeUTF8PlainText' was deprecated in iOS 15.0: Use UTTypeUTF8PlainText instead.

Posting a link this thread for the benefit of those reading along at home.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

So, I had a similar circular warning for this same method where I wanted to identify a JPG or a PNG image and create an image with the appropriate extension. Although a good example of how to use it was not clear to me and here's what worked for me (Objective C).

  1. Import the newer identifiers:
@import UniformTypeIdentifiers;
  1. Create a property to hold the identifiers:
@property (strong) NSString *imageType;
  1. Assign one of the identifiers to the property:
if ([theImageExtension isEqualToString:@"png"]) {

        _imageType = (NSString *)UTTypePNG.identifier;

    } else {

        _imageType = (NSString *)UTTypeJPEG.identifier;
    }
  1. Use it:
CGImageDestinationRef destinationRef = CGImageDestinationCreateWithURL((__bridge CFURLRef)theURL, (CFStringRef)_imageType, 1, NULL);

And that worked! (after many permutations)

In the example below for Swift users, "UTType.mpeg4Movie" replaces "kUTTypeMPEG4"

The dev group did a nice job cleaning up the syntax. The dev docs group dropped the ball by noting that that "typeIdentifierKey" is deprecated, but not mentioning that "contentTypeKey" is the intended replacement - https://developer.apple.com/documentation/foundation/urlresourcekey/1416354-typeidentifierkey. The "contentTypeKey" could refer to anything; "contentTypeIdentifierKey" or "contentUTIdentiferKey" would have made it several hours easier to figure out.

Note: the example extension of URL maybe applies solely to macOS and/or file URLs. Advance apologies for typos.

Sample usage for casual hobbyists like me:

let myFileURL = URL( ... appropriate initialization here ...) 
// unwrap as needed
if myFileURL.isMPEG4{
     //do stuff
}

Extension:

extension URL {

	// new style 
	var contentType: UTType? {
		return (try? resourceValues(forKeys: [.contentTypeKey]))?.contentType
	}

	var isMPEG4:Bool {
		guard let contentType = contentType else {return false}		// search for contentType failed
		return contentType.conforms(to: UTType.mpeg4Movie) ? true : false
	}

	// this is deprecated (macOS > 12?)
	// based on: https://stackoverflow.com/questions/28570627/how-to-find-file-uti-for-file-withouth-pathextension-in-a-path-in-swift
	var typeIdentifier: String? {
		return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
		// will need to convert output to CFString to test against UTTypeConformsTo
	}
	
	var DeprecatedIsMPEG4:Bool {

		guard let typeIdentifier = typeIdentifier else {return false}
		// test against the specific type
		if UTTypeConformsTo(typeIdentifier as CFString, kUTTypeMPEG4) {return true}
		return false
	}
}