我有一种方法可以使用用户提供的文件名保存UIScrollView的内容。
一切正常,但用户点击“保存”按钮并调用方法时会有很长的延迟。我无法弄清楚什么叫延迟,也找不到向用户表明一切正常的方法,我们还没有崩溃!
我认为延迟是在renderInContext期间发生的,但是当没有其他事情发生时,它似乎发生得更早。
这是一个麻烦的方法:
- (void)captureViewImage {
NSLog(@"captureViewImage called!");
// long delay happens here!
fileNamer.title = @"Preparing to save...";
// get user's file name
NSString *fileName = fileNamer.fileNameField.text;
// dismiss keyboard
[fileNamer.fileNameField resignFirstResponder];
// dismiss modal view
[self dismissFileNamingFormSheet];
CGRect oldFrame = mainScrollView.frame;
// capture off-screen content
mainScrollView.frame = CGRectMake(0, 0, 1024, 1432);
// make screenshot
UIGraphicsBeginImageContext(mainScrollView.bounds.size);
[mainScrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// save screenshot in docs dir
NSData *pngData = UIImagePNGRepresentation(screenImg);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
[pngData writeToFile:[documentsDir stringByAppendingPathComponent:fileName]
options:NSDataWritingAtomic error:nil];
// revert scroll view
mainScrollView.frame = oldFrame;
}
fileNamer是一个自定义类,它抛出一个UIModalPresentationFormSheet,要求用户提供该文件的名称。它看起来像这样:
@implementation FileNamingViewController
@synthesize fileNameField, newFileName;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil parent:(TestDriveViewController *) myParent {
if (self == [super initWithNibName:@"FileNamingViewController" bundle:nil]) {
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:@"Save"
style:UIBarButtonItemStyleDone
target:myParent
action:@selector(captureViewImage)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:@"Cancel"
style:UIBarButtonItemStyleBordered
target:myParent
action:@selector(dismissFileNamingFormSheet)];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
self.title = @"Save As?";
}
return self;
}
// UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
[fileNameField resignFirstResponder];
}
- (void)viewDidLoad {
[fileNameField becomeFirstResponder];
[super viewDidLoad];
}
fileNamer初始化并按如下方式发布:
- (void)presentFileNamingFormSheet {
fileNamer = [[FileNamingViewController alloc]
initWithNibName:nil
bundle:nil
parent:self];
fileNamingNavCtrl = [[UINavigationController alloc]
initWithRootViewController:fileNamer];
fileNamingNavCtrl.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:fileNamingNavCtrl
animated:YES];
// resize modal form sheet
fileNamingNavCtrl.view.superview.frame = CGRectMake(0, 0, 540, 115);
// reposition modal form sheet
CGPoint position = CGPointMake(self.view.center.x, self.view.center.y - 50);
fileNamingNavCtrl.view.superview.center = position;
}
- (void)dismissFileNamingFormSheet {
[fileNamer release];
[fileNamingNavCtrl release];
[self dismissModalViewControllerAnimated:YES];
}
Time Profiler的输出:
Running (Self) Symbol Name
1109.0ms 37.0% argb32_image_mark_rgb32
328.0ms 10.9% blkclr
171.0ms 5.7% lo_alltraps
134.0ms 4.4% pmap_enter
116.0ms 3.8% png_write_find_filter
102.0ms 3.4% pmap_remove_range
55.0ms 1.8% pmap_get_mapwindow
47.0ms 1.5% vm_page_lookup
47.0ms 1.5% ml_set_interrupts_enabled
43.0ms 1.4% vm_page_grab
38.0ms 1.2% OSAddAtomic64
34.0ms 1.1% hw_lock_to
31.0ms 1.0% alphaProviderGetBytes
30.0ms 1.0% hw_lock_unlock
26.0ms 0.8% png_read_filter_row
25.0ms 0.8% deflateInit_
23.0ms 0.7% vm_map_lookup_entry
23.0ms 0.7% adler32
22.0ms 0.7% memory_object_recover_named
答案 0 :(得分:0)
我发现了造成这种情况的原因。我保存的图像包含很多透明视图。例如,我有很多UIButtons的alpha值为0.05,使它们或多或少消失。我没有意识到我可以将按钮类型设置为自定义以使其不可见(我之前创建了视图)。一旦我将图像视图中的所有内容设置为alpha值为1.0,保存过程就会快得多。