UIDocument saveToURL:导致严重滞后

时间:2012-05-15 11:08:41

标签: objective-c ios cocoa-touch uidocument

UIDocument的saveToURL:forSaveOperation:completionHandler:假设是异步的(在文档中,它表示它在后台队列上执行此操作),但是我的代码在调用它时有7-10秒的延迟。

以下是代码:

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!

为什么在呼叫异步时存在这么大的延迟? 感谢

1 个答案:

答案 0 :(得分:1)

将文件写入磁盘是异步的,但不拍摄文档数据的快照。您应该使用仪器中的Time Profiler来了解实际需要的时间。我猜您可能需要优化contentsForType:error:(或等效)方法,但先测量一下!