gpt4 book ai didi

ios - 将图像添加到 UIView

转载 作者:可可西里 更新时间:2023-11-01 05:47:25 29 4
gpt4 key购买 nike

我正在尝试将图像添加到 UIView,然后将其作为 UIViewController 的 subview 添加。这是我的代码:

吹 View .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

我不知道哪里错了。我没有收到任何错误。我得到的只是一个空白的白色屏幕。任何帮助将不胜感激:)

最佳答案

在你的第一个代码片段中,你还没有将你的图像添加到 View 中,修改如下:

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;

关于ios - 将图像添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456424/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com