gpt4 book ai didi

iphone - 保存用相机拍摄的图像或从相机胶卷中选择的图像时出现长时间延迟 - iPhone

转载 作者:可可西里 更新时间:2023-11-01 05:06:32 26 4
gpt4 key购买 nike

我使用以下代码允许我的应用程序的用户拍摄/选择一张照片,然后将其保存到文档目录并设置为 UIImageView 的图像:

    -(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];
}

基本上我的问题是这段代码似乎执行得非常慢,例如,当我从相机胶卷中选择一张图像时,它最终会保存并将我带回调用 View ,但只是在很长的延迟之后..

任何人都可以阐明这一点吗?

最佳答案

保存大图像(如 iPhone 4/4S 中的相机拍摄的图像)需要很长时间。如果您分析该过程,您会发现 UIImagePNGRepresentation() 需要一段时间才能生成 PNG 图像,但根据我的经验,主要瓶颈是将 1+ MB 图像写入磁盘。

除了使用 JPEG 压缩(我发现在我的基准测试中速度稍快)或使用更快的第三方图像压缩例程之外,您几乎无法加快此过程。因此,如果您不想在发生这种情况时阻塞您的用户界面,请在后台线程或队列上分派(dispatch)此保存过程。您可以执行以下操作:

-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage* image = [info objectForKey: UIImagePickerControllerOriginalImage];
NSString* imageName = @"Receipt1Image1";
[self saveImage:image :imageName];
});
}

但是,请注意这些 UIImage 中的每一个都会占用相当多的内存来保存,因此您可能希望使用调度信号量来防止同时发生多个此类图像保存操作.

此外,作为文体说明,定义一个 Objective-C 方法,例如

- (void)saveImage:(UIImage*)image:(NSString*)imageName 

虽然允许,但非常不鼓励。为每个参数命名,如下所示:

- (void)saveImage:(UIImage*)image fileName:(NSString*)imageName 

这将使您的代码更具描述性。

关于iphone - 保存用相机拍摄的图像或从相机胶卷中选择的图像时出现长时间延迟 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com