2012-05-15 22 views
0

UIDocument的saveToURL:forSaveOperation:completionHandler:被假设是异步的(在文档中,它说,它将对背景队列这个动作),但我的代码有调用此当7-10秒落后。UIDocument saveToURL:造成严重滞后

下面的代码:

NSLog(@"saving image..."); 
[picDoc saveToURL:[picDoc fileURL] 
forSaveOperation:UIDocumentSaveForCreating 
completionHandler:^(BOOL success) { 


    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (success) { 
      NSLog(@"image save successful!"); 
      UIImage *thumbnail = [image imageByScalingAndCroppingForSize:CGSizeMake(146, 110)]; 
      Picture *picture = [[Picture alloc] initWithThumbnail:thumbnail filename:fileName]; 
      [[set pictures] addObject:picture]; 




      [self setupPictures]; 

      [scrollView scrollRectToVisible:((UIView *)[[scrollView subviews] lastObject]).frame animated:YES]; 
      currentIndex = [[set pictures] count] - 1; 
      [self showPicture]; 

     } else { 
      NSLog(@"failed saving image"); 
     } 
     [SVProgressHUD dismiss]; 
    }); 

    [picDoc closeWithCompletionHandler:nil]; 
}]; 
NSLog(@"exit"); 

和控制台:

2012-05-15 07:07:27.417 Picventory[5939:707] saving image... 
2012-05-15 07:07:34.120 Picventory[5939:707] exit 
2012-05-15 07:07:34.740 Picventory[5939:707] image save successful! 

为什么会出现如此巨大的滞后,当调用是异步的? 感谢

+0

你看看:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDocument_Class/UIDocument/UIDocument.html – CarlJ

回答

1

写入到磁盘文件是异步的,但服用该文件的数据的快照不是。你应该在乐器中使用时间分析器来找出真正需要这么长时间的东西。我猜你可能需要优化你的contentsForType:error:(或同等方法),但首先要测量!

+1

你是对的。才意识到这一点。我使用UIImagePNGRepresentation()转换为NSData,但速度太慢。现在使用JPG,速度更快。谢谢! – 0xSina