【问题标题】:How to fix the emplacement of the picture in xcode如何修复xcode中图片的位置
【发布时间】:2011-08-13 22:52:06
【问题描述】:

我在我的应用程序中使用 tabbar、navigationBar 和 SegmentedBar。

我必须像这样显示来自 HTTP 的图片:

- (void)viewDidLoad {
    [super viewDidLoad];
    // build the URL, perform the request and show the picture
    NSURL *url = [NSURL URLWithString: @"http://api.clementhallet.be/15avril.png"];

    //UIImage
    [[UIImageView alloc] initWithImage:image];
    image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
    image.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}

它正在运行,但图片不是我想要的 [确切][1]。

谢谢你的帮助!

【问题讨论】:

    标签: iphone objective-c uiimage nsurl image


    【解决方案1】:

    这应该是正确的顺序:

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    
    imageView.frame = CGRectMake(0, 0, 100, 100); 
    
    [self.view addSubview:imageView];
    

    您必须设置图像视图的框架以进行定位。如果没有在界面生成器中完成,请这样做:

    image.frame = CGRectMake(x, y, width, height);
    

    【讨论】:

    • image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; image.frame = CGRectMake(20, 20, 100, 100); [self.view addSubview:[[UIImageView alloc] initWithImage:image]]; ->在非结构或联合的情况下请求成员“框架”
    • 啊,我明白了,将您的图像嵌入 UIImageView ([[UIImageView alloc] initWithImage:image];) 或将其添加到 UIView,否则您将无法设置框架。
    • //UIImage [[UIImageView alloc] initWithImage:image]; image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; image.frame = CGRectMake(0, 0, 100, 100); //image.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>); [self.view addSubview:[[UIImageView alloc] initWithImage:image]]; 给我同样的问题...
    • 我在回答中添加了一个示例。请在您的问题中添加来源并对其进行格式化,在评论中无法重复。
    • 感谢您的支持。我已经更新了我所做的第一篇文章,但它不起作用。我必须做一个出口吗???因为此刻你是一个简单的视图(在我有一个 ImageView 但我刚刚删除它之前)
    猜你喜欢
    • 2016-10-02
    • 2022-01-11
    • 2019-12-07
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多