Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使图像出现在屏幕上?_Ios_Objective C_Uiimage - Fatal编程技术网

Ios 使图像出现在屏幕上?

Ios 使图像出现在屏幕上?,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我试图以编程的方式实例化一幅图像,我的最终目标是在屏幕上的不同位置水平放置一系列Empty.png。我不知道如何做到这一点,但首先我只是想让一个单一的图像出现在屏幕上。这是我在.m文件中的代码 - (void)drawAtPoint:(CGPoint)point { point = CGPointMake(100.0,100.0); UIImageView *img = [[UIImageView alloc] init]; img.image = [UIImage imageNamed:@"E

我试图以编程的方式实例化一幅图像,我的最终目标是在屏幕上的不同位置水平放置一系列Empty.png。我不知道如何做到这一点,但首先我只是想让一个单一的图像出现在屏幕上。这是我在.m文件中的代码

- (void)drawAtPoint:(CGPoint)point {
point = CGPointMake(100.0,100.0);
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:@"Empty.png"];
[self.view addSubview:img];

提前感谢您的帮助。

您采取的方法完全错误

作为初学者,如果你发现自己在绘制代码(
drawAtPoint:
drawInRect:
等),你几乎肯定是在错误的地方

特别是在加载和显示图像方面,iOS几乎为您完成了所有工作,因此您真的不必自己绘制任何东西

请不要误会,但请帮自己一个大忙,找一本关于这个主题的介绍性书籍。《大书呆子牧场指南》系列的书非常好,而且非常值得

编辑:

如果你真的不想买一本书(为了你自己,请买一本书——我买了,我很高兴我买了),这里有一个快速的方法应该有效

创建
UIImageView
的想法是正确的,但使用错误

你的应用程序中很可能有一个
UIViewController
。查找(或创建)
-(void)viewDidLoad
方法,并从此处显示图像:

- (void)viewDidLoad
{
    UIImage *myImage = [UIImage imageNamed:@"Empty"]; //you can leave out PNG.
    UIImageView *myFirstImageView = [[UIImageView alloc] initWithImage:myImage]; //this automatically gives the UIImageView the correct height and width
    [self.view addSubview:myFirstImageView]; //That's all. UIKit will handle displaying the imageView automatically.
}
这将在屏幕的左上角显示图像

通过在
UIImageView*myFirst…
之后插入以下行,可以轻松地将其移动:

myFirstImageView.center = CGPointMake(210.0, 345.0);
我有没有提到Big Nerd Ranch的书非常适合学习iOS开发,而且读起来也很有趣


此外,官方文档非常好(虽然没有那么有趣或容易阅读,也没有那么多解释)

你采取了一种完全错误的方法

作为初学者,如果你发现自己在绘制代码(
drawAtPoint:
drawInRect:
等),你几乎肯定是在错误的地方

特别是在加载和显示图像方面,iOS几乎为您完成了所有工作,因此您真的不必自己绘制任何东西

请不要误会,但请帮自己一个大忙,找一本关于这个主题的介绍性书籍。《大书呆子牧场指南》系列的书非常好,而且非常值得

编辑:

如果你真的不想买一本书(为了你自己,请买一本书——我买了,我很高兴我买了),这里有一个快速的方法应该有效

创建
UIImageView
的想法是正确的,但使用错误

你的应用程序中很可能有一个
UIViewController
。查找(或创建)
-(void)viewDidLoad
方法,并从此处显示图像:

- (void)viewDidLoad
{
    UIImage *myImage = [UIImage imageNamed:@"Empty"]; //you can leave out PNG.
    UIImageView *myFirstImageView = [[UIImageView alloc] initWithImage:myImage]; //this automatically gives the UIImageView the correct height and width
    [self.view addSubview:myFirstImageView]; //That's all. UIKit will handle displaying the imageView automatically.
}
这将在屏幕的左上角显示图像

通过在
UIImageView*myFirst…
之后插入以下行,可以轻松地将其移动:

myFirstImageView.center = CGPointMake(210.0, 345.0);
我有没有提到Big Nerd Ranch的书非常适合学习iOS开发,而且读起来也很有趣


此外,官方文档非常好(虽然没有那么有趣或容易阅读,也没有那么多解释)

对不起,我先前的回答错了。 我没意识到你会画画

不要使用图像视图。在这里,您应该继续使用核心图形。UIImageView对象可能会有所帮助,但只是其中的一部分

[img drawAtPoint:point]; 

我们应该做到这一点

对不起,我先前的回答错了。 我没意识到你会画画

不要使用图像视图。在这里,您应该继续使用核心图形。UIImageView对象可能会有所帮助,但只是其中的一部分

[img drawAtPoint:point]; 

我们应该做到这一点

您需要设置帧:

- (void)placeImageViewAtPoint:(CGPoint)point 
{
    UIImageView *img = [[UIImageView alloc] init];
    img.image = [UIImage imageNamed:@"Empty.png"];
    img.frame.origin = point;
    [self.view addSubview:img];  
}
我没有对此进行测试,但如果出现错误,您需要:

- (void)placeImageViewAtPoint:(CGPoint)point 
{
    UIImageView *img = [[UIImageView alloc] init];
    img.image = [UIImage imageNamed:@"Empty.png"];
    CGRect frame = img.frame;
    frame.origin = point;
    img.frame = frame;
    [self.view addSubview:img];  
}
您可能应该从ViewDidDisplay或类似的方法调用此方法:

-(void)viewDidAppear
{
    CGPoint point = point = CGPointMake(100.0,100.0);
    [self placeImageViewAtPoint:point];
}

您需要设置框架:

- (void)placeImageViewAtPoint:(CGPoint)point 
{
    UIImageView *img = [[UIImageView alloc] init];
    img.image = [UIImage imageNamed:@"Empty.png"];
    img.frame.origin = point;
    [self.view addSubview:img];  
}
我没有对此进行测试,但如果出现错误,您需要:

- (void)placeImageViewAtPoint:(CGPoint)point 
{
    UIImageView *img = [[UIImageView alloc] init];
    img.image = [UIImage imageNamed:@"Empty.png"];
    CGRect frame = img.frame;
    frame.origin = point;
    img.frame = frame;
    [self.view addSubview:img];  
}
您可能应该从ViewDidDisplay或类似的方法调用此方法:

-(void)viewDidAppear
{
    CGPoint point = point = CGPointMake(100.0,100.0);
    [self placeImageViewAtPoint:point];
}

这太低级了,只会让初学者陷入一大堆问题。如果您真的只想显示图像,可以使用
UIImageView
。只是不要在
drawRect:
drawAtPoint:
中执行此操作。imageView将为您处理绘图。这是对她的问题的直接回答。她已经在“DrawinMething”中,并且有一个错误,这修复了她的错误。坦率地说,由于她已经走了这么远,我不认为drawAtPoint是非常复杂的东西。相反地。这是相当基本的。这是太低级的东西了,它只会把初学者带入一大堆问题中。如果您真的只想显示图像,可以使用
UIImageView
。只是不要在
drawRect:
drawAtPoint:
中执行此操作。imageView将为您处理绘图。这是对她的问题的直接回答。她已经在“DrawinMething”中,并且有一个错误,这修复了她的错误。坦率地说,由于她已经走了这么远,我不认为drawAtPoint是非常复杂的东西。相反地。这是相当基本的。