Objective c GCD中央大调度异步在完成时崩溃
在我的应用程序中,我需要使用Grand Central Dispatch(CGD)在后台执行许多操作(调整一组图像的大小) 我有一个类Objective c GCD中央大调度异步在完成时崩溃,objective-c,ios,ipad,grand-central-dispatch,Objective C,Ios,Ipad,Grand Central Dispatch,在我的应用程序中,我需要使用Grand Central Dispatch(CGD)在后台执行许多操作(调整一组图像的大小) 我有一个类EngineViewController,它是应用程序的rootViewController: AppDelegate.m EngineViewController * engineVc = [[EngineViewController alloc]init]; self.window.rootViewController = engineVC; 在viewdi
EngineViewController
,它是应用程序的rootViewController:
AppDelegate.m
EngineViewController * engineVc = [[EngineViewController alloc]init];
self.window.rootViewController = engineVC;
在viewdide
方法的engineVC
中,我有以下代码:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//this method resize a set of image and save them in Document Folder of the app
//this method needs about 40 seconds to perform all operations
[[JSONParser sharedJSONParser]writeToDiskResizedImage];
NSLog(@"Finished");
});
}
方法[[JSONParser sharedJSONParser]writeToDiskResizedImage]
返回后,NSLog
在控制台中打印消息,应用程序崩溃。我所有的项目都是ARC,执行JSONParser
类。在控制台中没有错误消息;我只有这个:
从[[JSONParser sharedJSONParser]writeToDiskResizedImage]
执行的代码如下:
CGFloat screenScale = [UIScreen mainScreen].scale;
NSString * imagePath = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
//UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
UIImage * image = [[UIImage alloc]initWithContentsOfFile:imagePath];
CGSize newImageSize = [image resizeImageSettingWidth:(kArticleImageWidth * screenScale)];
UIGraphicsBeginImageContext(newImageSize);
[image drawInRect:CGRectMake(0,0,newImageSize.width,newImageSize.height)];
UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (image) {
[image release];
}
NSData *jpegData = UIImageJPEGRepresentation(newImage, 0.9f);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString * newImageName;
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
([UIScreen mainScreen].scale == 2.0)) {
newImageName = [NSString stringWithFormat:@"%@-article@2x.jpg", [imageName stringByDeletingPathExtension]];
}
else {
newImageName = [NSString stringWithFormat:@"%@-article.jpg", [imageName stringByDeletingPathExtension]];
}
NSLog(@"newImageName %@", newImageName);
NSString *filePath = [documentsPath stringByAppendingPathComponent:newImageName]; //Add the file name
[jpegData writeToFile:filePath atomically:YES]; //Write the file
if (newImage) {
[newImage release];
}
if (jpegData) {
[jpegData release];
}
您是否尝试设置异常断点?如果设置了异常断点,您应该会看到反汇编代码,并查看问题所在。如果未打开Instruments并搜索NSZombie,则您正在尝试释放、解除分配的对象,ARC可以插入自动释放的某个地方,当你释放某个东西时,ARC会再次释放它。考虑到这是在后台线程中发生的,我怀疑你调用的东西不是线程安全的。