2011-11-21 40 views
0

我在設置圖像時出現問題。我有一個委託人告訴我什麼時候從imagePickerController獲取圖像。它將它發送給所有者控制器,然後嘗試在另一個控制器上設置圖像。這一切似乎都應該起作用......但每當我看到視圖時,圖像就不存在了。下面的代碼:setImage不起作用

// this gets called when an image has been chosen from the library or taken from the camera 
// it is in the viewController in charge of grabbing an image (.m file) 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    if(self.delegate) 
    { 
     [self.delegate didAcquirePicture:image]; 
    } 
} 


//This is in the controller .m file 
- (void)didAcquirePicture:(UIImage *)picture 
{ 
    if(picture != Nil) 
    { 
     self.photoEditView = [[PhotoEditViewController alloc] init]; 
     [self.photoPicker.imagePickerController dismissModalViewControllerAnimated:NO]; 
     [self.photoEditView.imageView setImage:picture]; 
     [self presentModalViewController: self.photoEditView animated:NO]; 

    } 
} 

//this is the photoEditViewController .h 
#import <UIKit/UIKit.h> 

@class PhotoEditViewController; 

@protocol PhotoEditViewControllerDelegate 
@end 


@interface PhotoEditViewController : UIViewController { 
    IBOutlet UIImageView *imageView; 
} 

@property (retain) UIImageView *imageView; 


@end 

//this is the photoEditView .m file 
#import "PhotoEditViewController.h" 

@implementation PhotoEditViewController 

@synthesize imageView; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc { 
    [super dealloc]; 
    [imageView dealloc]; 
} 

@end 
+0

在' - imagePickerController ::',你是積極的'圖像'不是'無'?你正在檢查' - didAcquirePicture:'中的'nil'(或者更確切地說'Nil'),但是你甚至不會給自己一個'NSLog'來驗證是否正在執行所需的代碼。在繼續之前,我會仔細檢查一下「無」的值。 –

+0

僅供參考,甚至會擴展到相同的東西,Nil旨在表示NULL,而不是對象...使用nil代替,以提高可讀性... – Macmade

+0

不確定圖像問題,但我在代碼中看到一些問題。在你實現的dealloc方法中,釋放你的實例變量,然後調用[super dealloc]。如果您沒有將@EditView作爲@property(retain等)聲明,那麼下一部分不是問題。 self.photoEditView = [[PhotoEditViewController alloc] init]將會泄漏,如果該屬性在設置時保留它...你可以使用self.photoEditView = [[[PhotoEditViewController alloc] init] autorelease];如果屬性設置爲保留,則避免內存泄漏 –

回答

1

想出問題


事實證明,我的圖像正被釋放前的視圖完全加載並控制被返還給事件循環。這是一個計時問題,我很愚蠢,看不到它。所以,我在叫tempImage .h文件中作出一個UIImage變量,然後我只是把這個在 - (無效)viewDidLoad中:

self.imageView.image = tempImage; 

這是我的didAcquirePicture方法貌似現在:

- (void)didAcquirePicture:(UIImage *)picture 
{ 
    if(picture != nil) 
    { 
     self.photoEditView = [[[PhotoEditViewController alloc] init] autorelease]; 

     self.photoEditView.tempImage = picture; 

     [self.photoPicker.imagePickerController dismissModalViewControllerAnimated:NO]; 
     [self presentModalViewController: self.photoEditView animated:NO]; 
     [self.photoEditView setupStache]; 
    } 
} 

我基本上只是保存圖像,然後當我知道視圖被加載時,我在我的viewDidLoad中設置圖像。感謝大家幫助我得出最終答案!

1

看起來這是可能的,當您關閉imagePickerController圖像被釋放出來,你把它分配給ImageView的面前。如果是這樣,你可以用

[[picture retain] autorelease];

修復如前所述,測試針對nil沒有Nil。另外,最好使用[self.delegate respondsToSelector: @selector(didAcquirePicture)]; - 請記住發送消息至nil是安全的。