1

I want to show pasteboard content in an UIImageview if pasteboard contains image.

How can I do it? I tried with UIPasteboard, but how can I check which type of image it contains?

János
  • 32,867
  • 38
  • 193
  • 353

1 Answers1

0

You don't need the type of data to show the image, just do:

UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyPaste" create:YES];
NSData *data = [appPasteBoard dataForPasteboardType:@"com.yourCompany.yourApp.yourType"];
imageView.image = [UIImage imageWithData:data];

However, you could guess the type by looking at the first byte (source):

+ (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
    case 0xFF:
        return @"image/jpeg";
    case 0x89:
        return @"image/png";
    case 0x47:
        return @"image/gif";
    case 0x49:
    case 0x4D:
        return @"image/tiff";
    }
    return nil;
}
Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • why don't you use only like this? `let image = UIPasteboard.generalPasteboard().image! let data = UIImagePNGRepresentation(image)` – János Jun 29 '15 at 15:41
  • `UIPasteboard.generalPasteboard().items` is what you should be looking for. This contains information about the image and image itself. https://developer.apple.com/documentation/uikit/uipasteboard/1622067-items?language=objc – iCoder Jan 21 '20 at 03:18