【问题标题】:ios7 CGRect property not found on object在对象上找不到 ios7 CGRect 属性
【发布时间】:2014-07-16 01:22:02
【问题描述】:

我尝试使用以下代码对 UIImage 的尺寸进行 NSLog 记录,但收到警告“在类型的对象上找不到属性 'theImage'。”我做错了什么?

UIImage *theImage = [UIImage imageNamed:@"my_image.jpg"];

CGRect imageContent = self.theImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;

NSLog(@"Size of my Image => %f, %f ", imageWidth, imageHeight);

【问题讨论】:

    标签: uiimage nslog cgrect


    【解决方案1】:

    你应该这样编码:

    UIImage *theImage = [UIImage imageNamed:@"my_image.jpg"];
    CGSize imageContent = theImage.size;
    CGFloat imageWidth = imageContent.width;
    CGFloat imageHeight = imageContent.height;
    
    NSLog(@"Size of my Image => %f, %f ", imageWidth, imageHeight);
    

    【讨论】:

    【解决方案2】:

    这正是它所说的;无论您将该代码放入的哪个类都没有名为theImage 的属性。您可能的意思是:

    CGSize imageContent = theImage.size;
    CGFloat imageWidth = imageContent.width;
    CGFloat imageHeight = imageContent.height;
    

    使用self. 表示法访问该类实例的属性。局部变量是一个单独的概念,因此以单独的方式访问。实例变量与类的实例具有相同的生命周期。局部变量只有封闭作用域(本例中的方法)的生命周期。

    此外,根据 user3779315,UIImage 有一个 size,但没有 bounds。那是因为它没有origin。它不是任何布局层次结构的一部分;它是独立的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 2012-04-07
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多