测试特定图像是否已复制到粘贴板
Testing that a particular image was copied to pasteboard
我正在编写一个测试来验证将图像复制到粘贴板的功能。
这是测试,我更愿意这样写:
// reset the paste board
UIPasteboard.generalPasteboard.image = nil; //<-- this explodes
XCTAssertNil(UIPasteboard.generalPasteboard.image);
// Grab some random existing image
UIImage *image = [UIImage imageNamed:@"some-image"];
MJKMyClass *myInstance = [[myInstance alloc] initWithImage:image];
[myInstance doSomethingThatCopiesImageToPasteboard]
XCTAssertNotNil(UIPasteboard.generalPasteboard.image);
这失败了:
failed: caught "NSInvalidArgumentException", "-[UIPasteboard setImage:]: Argument is not an object of type UIImage [(null)]"
这令人惊讶,因为根据 UIPasteboard header,图像是一个可为空的字段。
@interface UIPasteboard(UIPasteboardDataExtensions)
<...>
@property(nullable,nonatomic,copy) UIImage *image __TVOS_PROHIBITED;
<...>
@end
我认为这意味着他们正在对参数进行运行时检查,即使它可以为空。
我尝试过的事情:
- 通过 id 比较 objects 不起作用,因为 UIImage 被 generalPastboard.image 复制(每次调用 UIPasteboard.generalPasteboard.image 都可以是不同的实例)
- 通过 PNG 表示进行比较可能有效,但看起来很粗糙。
- 到目前为止,按图像大小进行比较是我最接近的选择,但似乎也是迂回的。
您可以使用 items
属性:
清除粘贴板而无需通过 nil
UIPasteboard.generalPasteboard.items = @[];
或在Swift中:
UIPasteboard.generalPasteboard().items = []
为了比较 UIImage,您可能需要查看其他一些问题:
- How to compare two UIImage objects
- https://whosebug.com/search?q=uiimage+compare
我正在编写一个测试来验证将图像复制到粘贴板的功能。
这是测试,我更愿意这样写:
// reset the paste board
UIPasteboard.generalPasteboard.image = nil; //<-- this explodes
XCTAssertNil(UIPasteboard.generalPasteboard.image);
// Grab some random existing image
UIImage *image = [UIImage imageNamed:@"some-image"];
MJKMyClass *myInstance = [[myInstance alloc] initWithImage:image];
[myInstance doSomethingThatCopiesImageToPasteboard]
XCTAssertNotNil(UIPasteboard.generalPasteboard.image);
这失败了:
failed: caught "NSInvalidArgumentException", "-[UIPasteboard setImage:]: Argument is not an object of type UIImage [(null)]"
这令人惊讶,因为根据 UIPasteboard header,图像是一个可为空的字段。
@interface UIPasteboard(UIPasteboardDataExtensions)
<...>
@property(nullable,nonatomic,copy) UIImage *image __TVOS_PROHIBITED;
<...>
@end
我认为这意味着他们正在对参数进行运行时检查,即使它可以为空。
我尝试过的事情:
- 通过 id 比较 objects 不起作用,因为 UIImage 被 generalPastboard.image 复制(每次调用 UIPasteboard.generalPasteboard.image 都可以是不同的实例)
- 通过 PNG 表示进行比较可能有效,但看起来很粗糙。
- 到目前为止,按图像大小进行比较是我最接近的选择,但似乎也是迂回的。
您可以使用 items
属性:
nil
UIPasteboard.generalPasteboard.items = @[];
或在Swift中:
UIPasteboard.generalPasteboard().items = []
为了比较 UIImage,您可能需要查看其他一些问题:
- How to compare two UIImage objects
- https://whosebug.com/search?q=uiimage+compare