如何在 iOS 中显示图像是否在粘贴板上
How to show if image is on pasteboard in iOS
如果粘贴板包含图像,我想在 UIImageview
中显示粘贴板内容。
我该怎么做?我尝试使用 UIPasteboard
,但如何检查它包含哪种类型的图像?
您不需要显示图像的数据类型,只需执行以下操作:
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;
}
如果粘贴板包含图像,我想在 UIImageview
中显示粘贴板内容。
我该怎么做?我尝试使用 UIPasteboard
,但如何检查它包含哪种类型的图像?
您不需要显示图像的数据类型,只需执行以下操作:
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;
}