2011-11-22 77 views
2

我写了最简单的iPhone应用程序。它有一个单一的视图窗口。该主视图包含图像视图和2个按钮,分别标记为“图库中的图片”和“拍照”。应用程序会根据所选按钮生成图像,并将其作为UIImageView的图像。以获取图像的代码如下:由UIImagePickerViewController返回的图像不显示

- (void) onCapturePicture:(BOOL)fromLibrary 
{ 
    UIImagePickerController *controller = [[UIImagePickerController alloc] init]; 
    [controller setDelegate: self]; 
    [controller setSourceType: fromLibrary ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera]; 
    [controller setAllowsEditing: YES]; 
    [self presentModalViewController: controller animated: YES]; 
    [self setImagePickerController: controller]; 
} 

在图像被返回并分配给UIImageView控制委托函数如下:

- (void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *pickedImage = [info objectForKey: UIImagePickerControllerEditedImage]; 

    [_pictureView setImage: pickedImage]; 
    [[self view] setNeedsDisplay]; 
    [self dismissModalViewControllerAnimated: YES]; 
} 

我也试图与UIImagePickerControllerOriginalImage与相同的结果,即:当照片来自照片库时,它会显示为UIImageView中的图像。使用相机拍摄照片时,即使指向返回图像的指针不为零,它也完全不显示。如果我使用

- (void) imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image 
        editingInfo:(NSDictionary *)editingInfo 

代替,一切正常,但文件表明它是过时的,我想这可能是对被拒绝我的应用程序的一个原因。

另外,我有2个与主题相关的其他问题:

  1. UIPickerController需要很长的时间来显示第一次采摘从照片库中的图片时,它的调用(如11秒) 。有什么办法可以减少这种加载时间,并带来一些窍门? (在空闲时间静静地预加载什么)?

  2. 只要照相机打开,应用程序就会收到内存警告(接收到的内存警告,级别= 1)。有没有办法避免它,或者我做错了什么?这是一个带2个按钮的普通香草应用程序。基本上我为这个应用程序输入的代码90%列在这篇文章中。只要打开相机就会导致这种情况发生。我怎样才能避免这个问题?我在UIImagePickerController上阅读了其他有关内存泄漏的帖子,但这不适用于这种情况。第一次执行代码以获取相机的图片时,我收到内存警告。

在此先感谢。

回答

1

您可能需要更改这些行:

[[self view] setNeedsDisplay]; 
[self dismissModalViewControllerAnimated: YES]; 

到:

[self dismissViewControllerAnimated:YES completion:nil]; 

正如我不相信你需要刷新显示。我建议你改变解雇方法,只是因为我测试了它,它的工作原理。祝你好运。