2011-12-10 127 views
4

我試圖將圖像添加到一個UIView,然後將其添加爲上的UIViewController子視圖.. 這裏是我的代碼:添加圖片到一個UIView

BlowView.h

#import <UIKit/UIKit.h> 

@interface BlowView : UIView { 
    UIImageView *stone; 
} 

@property (nonatomic, retain) UIImageView *stone; 

- (id)init; 
@end 

BlowView .M

#import "BlowView.h" 

@implementation BlowView 

@synthesize stone; 

- (id)init { 

    UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Placard.png"]]; 
    stone = logoView; 
    [logoView release]; 

    UIImage *img = [UIImage imageNamed:@"Placard.png"]; 
    CGRect frame = CGRectMake(0, 0, img.size.width, img.size.height); 


    [self initWithFrame:frame]; 

    return self; 
} 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if(self) { 

    } 
    return self; 
} 

- (void)dealloc { 
    [stone release]; 
    [super dealloc]; 
} 


@end 

BlowViewController.h

@class BlowView; 

@interface BlowViewController : UIViewController { 
    BlowView *aview; 
} 

@property (nonatomic, retain) BlowView *aview; 

-(void)setUpView; 
@end 

BlowViewController.m

#import "BlowViewController.h" 
#import "BlowView.h" 


@implementation BlowViewController 

@synthesize aview; 

- (void)setUpView { 

    // Create the placard view -- its init method calculates its frame based on its image 
    BlowView *aView2 = [[BlowView alloc] init]; 
    self.aview = aView2; 
    [aView2 release]; 
    self.view = self.aview; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 




- (void)viewDidLoad { 

    [super viewDidLoad]; 
    [self setUpView]; 

    aview.center = self.view.center; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)dealloc { 
    [aview release]; 
    [super dealloc]; 
} 
@end 

想不通我要去的地方錯了。我沒有得到任何錯誤..我得到的只是一個空白的白色碎片。任何幫助將不勝感激:)

回答

3

在你的第一個代碼段,你有沒有你的圖像添加到視圖,修改如下:

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Placard.png"]]; 
stone = logoView; 
[logoView release]; 

UIImage *img = [UIImage imageNamed:@"Placard.png"]; 
CGRect frame = CGRectMake(0, 0, img.size.width, img.size.height); 


[self initWithFrame:frame]; 

// Here add your imageView(stone) to your view as a subview 
[self addSubview:stone]; 

return self; 
+0

出人意料的並沒有工作和應用程序與此崩潰「 [UIImageView superview]:發送到釋放實例的消息0x10167fd0「 –

+0

我通過在添加子視圖後釋放了logoView實例來解決此問題..但是,我希望整個幀僅由圖像組成,以便在執行aview時。 center = CGPointMake(x,y)..它移動圖像而不是整個視圖 –