2011-04-29 50 views
9

我有一個應用程序,需要一張照片,並把它放在圖像視圖。簡單。 代碼如下所示:用iPhone拍照,然後使用它!

- (void)takePhoto:(id)sender 
{ 
    // Lazily allocate image picker controller 
    if (!imagePickerController) { 
     imagePickerController = [[UIImagePickerController alloc] init]; 

     // If our device has a camera, we want to take a picture, otherwise, we just pick from 
     // photo library 
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 
      [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; 
     }else 
     { 
      [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     } 

     // image picker needs a delegate so we can respond to its messages 
     [imagePickerController setDelegate:self]; 
    } 
    // Place image picker on the screen 
    [self presentModalViewController:imagePickerController animated:YES]; 
} 

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

    image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)]; 

    [imageView setImage:image]; 

    [self dismissModalViewControllerAnimated:YES]; 
} 

當我用相機膠捲一切都很正常,但如果我用實際攝像機的圖像視圖僅僅是黑色的。這是爲什麼?

在圖像視圖中使用它之前,是否需要將它保存到相機膠捲?

+0

設置斷點檢查,如果當它傳遞給ImageHelpers – edo42 2011-04-30 11:51:02

+0

不,它似乎不無圖像是零。我可以使用以下命令將照片保存到相機膠捲:UIImageWriteToSavedPhotosAlbum(),使圖像存在於變量中。當我將它分配給imageView時,它似乎迷失了方向。 – Linus 2011-05-01 12:55:02

+0

我想我必須在將它發送到我的視圖之前將它保存在某處。這裏最好的方法是什麼? – Linus 2011-05-01 13:03:29

回答

6

好的。我自己找到解決方案。

我只好先解僱模態視圖...

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

    [self dismissModalViewControllerAnimated:YES]; //Do this first!! 
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 

    image = [ImageHelpers imageWithImage:image scaledToSize:CGSizeMake(480, 640)]; 

    [imageView setImage:image]; 
} 
1

爲什麼你不能只使用該代碼?

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [imageView setImage:image]; 
    [picker dismissModalViewControllerAnimated:YES]; 
    [picker release]; 
} 
+1

那麼,它已被棄用在iOS 3 ... http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/DeprecationAppendix/AppendixAprendixAeprecatedAPI.html#//apple_ref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPickingImage:editingInfo: – Linus 2011-04-29 17:31:48

+0

XCode從未警告過我... – edo42 2011-04-29 17:54:07

+0

好的。我仍然不想使用棄用的函數;) – Linus 2011-04-29 20:42:27