我使用以下代碼來允許我的應用程序的用戶拍攝/選擇照片,然後將其保存到文檔目錄並設置爲一個UIImageView的形象:保存使用相機拍攝的圖像或從相機膠捲中選擇的長時間延遲 - iPhone
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.tag == 0){
if (buttonIndex == 0) {
NSLog(@"Take Picture Button Clicked");
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
}
else if (buttonIndex == 1) {
NSLog(@"Choose From Library Button Clicked");
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// Delegate is self
imagePicker.delegate = self;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
}
else if (buttonIndex == 2) {
NSLog(@"Cancel Button Clicked");
}
}
......
- (void)saveImage:(UIImage*)image:(NSString*)imageName
{
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
receiptImageView1.image = [UIImage imageWithContentsOfFile:fullPath];
self.receiptImage1 = fullPath;
NSLog(@"image saved");
}
//Receive the image the user picks from the image picker controller
-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info {
UIImage* image = [info objectForKey: UIImagePickerControllerOriginalImage];
NSString* imageName = @"Receipt1Image1";
[self saveImage:image :imageName];
}
基本上我的問題是,這種代碼似乎非常緩慢執行,例如,當我選擇從相機膠捲的圖像它最終保存,給我回調用視圖,但只有經過很長的延遲..
任何人都可以在這方面的任何指示?
使用Time Profiler工具可以找出哪些部件是最慢的,並嘗試並優化這些部件。 –