如果粘贴板包含图片,我想在UIImageview
中显示粘贴板内容。
我该怎么办?我尝试使用UIPasteboard
,但我该如何检查它包含哪种类型的图像?
答案 0 :(得分:0)
您不需要显示图像的数据类型,只需执行以下操作:
UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyPaste" create:YES];
NSData *data = [appPasteBoard dataForPasteboardType:@"com.yourCompany.yourApp.yourType"];
imageView.image = [UIImage imageWithData:data];
但是,您可以通过查看第一个字节(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;
}