Add image to UIView

I am trying to add an image in a UIView and then add it as a subview on a UIViewController .. here is my code:

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. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)dealloc { [aview release]; [super dealloc]; } @end 

can't understand where i'm wrong. I do not get any errors. All I get is pure white. Any help would be greatly appreciated :)

+4
source share
1 answer

In your first code snippet, you did not add your image to the view, change below:

 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; 
+3
source

Source: https://habr.com/ru/post/1385647/


All Articles