Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimeTyp that can't be recognise #13

Open
turk-jk opened this issue Mar 11, 2019 · 3 comments
Open

mimeTyp that can't be recognise #13

turk-jk opened this issue Mar 11, 2019 · 3 comments

Comments

@turk-jk
Copy link

turk-jk commented Mar 11, 2019

Hi there,
I'm using Xcode 10.1
swift 4.2 tested on simulator, iphone and playground

when I init an UTI
let uti = UTI("com.apple.uikit.image") print(uti.declaration)

it prints // [:]

- Background of the problem

the reason I need this because when I copy an image from WhatsApp, WhatsApp will add one item that has three items in the
`let paste = UIPasteboard.general.item // type of [[String : Any]]

print (paste)

// [["public.png": <UIImage: 0x2821b5730>, {1024, 1017}, "public.jpeg": <UIImage: 0x2821e5c70>, {1024, 1017}, "com.apple.uikit.image": <OS_dispatch_data: data[0x2810a6c40] = { leaf, size = 1015803, buf = 0x11c6fc000 }>]]`

you can see WhatsApp add
[["public.png" "public.jpeg", "com.apple.uikit.image"]]

I always take only the first item of those three and their order changes sometimes, the problem occur when "com.apple.uikit.image" is the first item

thanks

@cockscomb
Copy link
Owner

@turk-jk Hi.

I could reproduce the behavior.

// Create UIImage
let renderer =  UIGraphicsImageRenderer(size: CGSize(width: 10, height: 10))
let image = renderer.image { _ in }

// Set image to pasteboard
let pasteboard = UIPasteboard.general
pasteboard.image = image

// Reproduced here
for item in pasteboard.items {
    let UTIs = item.keys.map(UTI.init)
    print(UTIs) // [public.png, public.jpeg, com.apple.uikit.image]
}

I think that com.apple.uikit.image is the UIKit internal UTI.

You should filter that.

let imageType = UTI(kUTTypeImage as String)
for item in pasteboard.items {
    // Filter by conformance
    let UTIs = item.keys.map(UTI.init).filter { imageType ~= $0 }
    print(UTIs) // [public.png, public.jpeg]
}

@turk-jk
Copy link
Author

turk-jk commented Mar 12, 2019 via email

@turk-jk
Copy link
Author

turk-jk commented Mar 12, 2019

I have just tested your code which works fine, but doesn't serve my app purpose well as it receive many different type of data not only kUTTypeImage
what I did is this

for item in pasteboard.items {
    print("item has keys \(item.keys)")
    let validItems = item.filter { UTI($0.key).filenameExtension != nil }
    print("validItems has keys \(item.keys)")
}

prints

item has keys ["com.apple.uikit.image", "public.png", "public.jpeg"]
validItems has keys ["public.png", "public.jpeg"]

Now I understand that I am relaying on the condition validUTI.filenameExtension != nil
would you have a better suggestion ?

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants