我有一个Today小部件扩展,其中viewDidLoad:
方法,我调用方法unloadClipboard:
,该方法包含一个被调用的位,它将用户的图像剪贴板转换为NSData。理论上的一切都应该可以正常工作,但是,小部件在这段代码上崩溃,因为初始化需要很长时间 - 即使我在后台线程上运行它。
if (copiedImage && self.imageFunctionalityEnabled) {
//Item is an image, get data
dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
NSData *data = UIImageJPEGRepresentation([[UIPasteboard generalPasteboard] image], 0.032);
//If data isn't already added
if (![imageArray containsObject:data]) {
[imageArray addObject:data];
}
//Sync the defaults
[defaults setObject:imageArray forKey:@"imagesArray"];
[defaults synchronize];
UIImage *image = [UIImage imageWithData:[imageArray lastObject]];
dispatch_async(dispatch_get_main_queue(), ^{
_imageView.image = image;
});
});
} else if (copiedString && ![textArray containsObject:copiedString]) {
[textArray addObject:copiedString];
[defaults setObject:textArray forKey:@"array"];
[defaults setObject:imageArray forKey:@"imagesArray"];
_imageView.image = [UIImage imageWithData:[imageArray lastObject]];
} else {
[defaults setObject:imageArray forKey:@"imagesArray"];
_imageView.image = [UIImage imageWithData:[imageArray lastObject]];
}
在一些断点,NSLogs和时间分析小部件目标之后,我得出结论,这条线,据说需要花费太长时间。
NSData *data = UIImageJPEGRepresentation([[UIPasteboard generalPasteboard] image], 0.032);
以下是崩溃报告的示例:
异常类型:EXC_CRASH(SIGABRT)异常子类型:LAUNCH_HANG 异常消息:扩展花了太多时间来初始化
我已成功地通过注释掉该块中的代码来成功地隔离了该问题,其中窗口小部件运行正常。任何帮助表示赞赏。
更新:以下是包含更改的代码:
if (copiedImage && self.imageFunctionalityEnabled) {
//Item is an image, get data
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = UIImageJPEGRepresentation(copiedImage, 0.032);
//If data isn't already added
if (![imageArray containsObject:data]) {
[imageArray addObject:data];
}
//Sync the defaults
[defaults setObject:imageArray forKey:@"imagesArray"];
[defaults synchronize];
UIImage *image = [UIImage imageWithData:[imageArray lastObject]];
dispatch_async(dispatch_get_main_queue(), ^{
_imageView.image = image;
});
});
} else if (copiedString && ![textArray containsObject:copiedString]) {
[textArray addObject:copiedString];
[defaults setObject:textArray forKey:@"array"];
[defaults setObject:imageArray forKey:@"imagesArray"];
_imageView.image = [UIImage imageWithData:[imageArray lastObject]];
} else {
[defaults setObject:imageArray forKey:@"imagesArray"];
_imageView.image = [UIImage imageWithData:[imageArray lastObject]];
}
以下是日志:
Dec 1 18:41:57 Isa-Ranjhas-iPhone ReportCrash [7074]: 为流程CopyfeedWidget制定崩溃报告[7073] Dec 1 18:41:57 Isa-Ranjhas-iPhone CopyfeedWidget [7073]:VIEW WILL APPEAR Dec 1 18:41:57 Isa-Ranjhas-iPhone SpringBoard [328]: 插件com.isaranjha.Copyfeed.CopyfeedWidget在12月1日中断 18:41:57 Isa-Ranjhas-iPhone SpringBoard [328]:插件 com.isaranjha.Copyfeed.CopyfeedWidget于12月1日18:41:57失效 Isa-Ranjhas-iPhone mediaserverd [22]:''(pid = 7073)设置 DiscoveryMode = DiscoveryMode_None,currentDiscoveryMode = DiscoveryMode_PresenceScan Dec 1 18:41:57 Isa-Ranjhas-iPhone 断言[63]:pid_suspend失败:未知错误:-1,未知错误:-1 Dec 1 18:41:57 Isa-Ranjhas-iPhone断言[63]:断言失败:12B436: 断言+ 57724 [10EC04AD-D18F-3ECD-8A8E-FBB1CC4E2BD6]:0x3 Dec 1 18:41:57 Isa-Ranjhas-iPhone断言[63]:无法设定 优先级为2, 优先:没有这样的过程Dec 1 18:41:57 Isa-Ranjhas-iPhone 断言[63]:无法将优先级设置为4096,优先级:没有这样的过程12月1日18:41:57 Isa-Ranjhas-iPhone SpringBoard [328]:带标识符的小部件 'com.isaranjha.Copyfeed.CopyfeedWidget'意外断开了12月 1 18:41:57 Isa-Ranjhas-iPhone pkd [5114]:分配插件 com.isaranjha.Copyfeed.CopyfeedWidget(1.0)插件沙箱Dec 1 18:41:57 Isa-Ranjhas-iPhone pkd [5114]:启用pid = 328 for 插件com.isaranjha.Copyfeed.CopyfeedWidget(1.0) CAF48DC4-9AD3-4E4F-82F5-F77903676E1F /private/var/mobile/Containers/Bundle/Application/E12BF21F-02A5-4E06-8D4C-CA2DEBCA3850/Copyfeed.app/PlugIns/CopyfeedWidget.appex