[IOS14] How to get Data type image data directly from PHPickerViewController?

PHPickerViewController

Looking at Apple's PHPickerViewController sample code, there is an example of getting a UIImage, but there is no example of getting Data. ..

Of course, converting a UIImage to Data is subtle.

It may be good to give up a hundred steps and use HEIC, JPG or PNG, but the method to convert UIImage to Data is "[pngData ()](https://developer.apple.com/documentation/uikit/uiimage/1624096". There are only two, "-png data)" and "jpegData (compressionQuality :)". If you convert the GIF with these methods, the animation will stop.

After all, I want to get the Data type directly. For such a person, I will write ** How to get Data from PHPickerViewController **.

You can get the data of the image by using loadDataRepresentation (forTypeIdentifier: completionHandler :) of NSItemProvider.

You can get it by using the NSItemProvider loadDataRepresentation (forTypeIdentifier: completionHandler :).

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    results.forEach { result in
        result.itemProvider.loadDataRepresentation(forTypeIdentifier: "public.image", completionHandler: { [weak self] data, _ in
            guard let data = data else { return }
            print(data)
        })
    }
}

The trick is to specify public.image for the forTypeIdentifier (Uniform Type Identifier). By doing this, you can get Data such as HEIC, PNG, JPG, GIF.

If you want to specify the data to be acquired in more detail, use Apple's Uniform Type Identifier Concepts. Please specify for reference.

Recommended Posts

[IOS14] How to get Data type image data directly from PHPickerViewController?
[IOS] How to get the table name from AWS DynamoDB
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path
How to get a heapdump from a Docker container
How to get Class from Element in Java
How to get SIMD optimization from HotSpot JavaVM
I want to get only the time from Time type data ...! [Strftime] * Additional notes
How to get the date from the JavaScript Date type that C # developers are addicted to
How to type backslash \
How to deploy to Heroku from a local docker image
How to play YouTube videos directly on iOS app
How to pass image pixel information natively from Unity
How to get jdk etc from oracle with cli
How to write a migration from Rails datetime type to date type
[iOS] [Objective-C] How to update a widget from an Objective-C app
[iOS] How to read Carthage
How to migrate from JUnit4 to JUnit5
How to delete only specific data from data created using Form object
[Swift UI] How to get the startup status of the application [iOS]
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
How to use Java HttpClient (Get)
How to get started with slim
How to use Spring Data JDBC
[How to install Spring Data Jpa]
Get GTFS data from OpenTripPlanner Graph
How to get along with Rails
Get gcr image from local kubernetes
How to distinguish ubuntu cloud image
How to use Java enum type
How to change from HTML to Haml
Try to get data from database using MyBatis in Micronaut + Kotlin project
Get the selected image as Data with PHPickerViewController (and handle Live Photo)
How to get the setting value (property value) from the database in Spring Framework
Call the Microsoft Emotion API by sending image data directly from Java.
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
[Rails] How to convert from erb to haml
How to blur an image (super easy)
[Java] How to add data to List (add, addAll)
[Note] How to get started with Rspec
How to call Swift 5.3 code from Objective-C
[Java] How to get the current directory
[Flutter] How to use C / C ++ from Dart?
Java: How to send values from Servlet to Servlet
To write Response data directly in Spring
How to map tsrange type in Hibernate
How to get the date in java
How to delete data with foreign key
Introduction to kotlin for iOS developers ④-Type
How to implement image posting using rails
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to overwrite Firebase data in Swift
Type conversion from java BigDecimal type to String type
[Rails] How to handle data using enum
How to assemble JSON directly in Jackson
[iOS] How to make UI parts added from code fit in Safe Area
How to create a route directly from the URL you want to specify + α
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to get an arbitrary digit from a number of 2 or more digits! !!