【问题标题】:Can any of the iPhone collection objects hold an image?任何 iPhone 集合对象都可以保存图像吗?
【发布时间】:2008-10-08 05:15:18
【问题描述】:

实际上,我想要一个包含 2 个图像对象和 1 个文本对象的自定义单元格,我决定为这些对象创建一个容器。

那么是否可以在对象中保存图像并将该对象插入任何集合对象中,然后使用该对象显示在单元格内?

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    NSArray 和 NSDictionary 都保存对象。这些很可能是您将与表格视图一起使用的集合。

    实现你想要做的最好的方法是使用 UIImage 类。 UIImages 包装了一个 CGImage 并为你做所有的内存管理(如果你的应用程序内存不足,图像数据将被清除并在你绘制它时自动重新加载 - 很酷,对吧?)你也可以很容易地从文件中读取图像使用这个类(支持一大堆格式)。

    查看 NSArray、NSMutableArray 和 UIImage 的文档了解更多信息。

    //create a UIImage from a jpeg image
    UIImage *myImage = [UIImage imageWithContentsOfFile:@"myImage.jpg"];
    NSArray *myArray = [NSMutableArray array];      // this will autorelease, so if you need to keep it around, retain it
    [myArray addObject:myImage];
    
    
    
    //to draw this image in a UIView's drawRect method
    CGContextRef context = UIGraphicsGetCurrentContext();  // you need to find the context to draw into
    UIImage *myImage = [myArray lastObject];   // this gets the last object from an array, use the objectAtIndex: method to get a an object with a specific index
    CGImageRef *myCGImage = [myImage CGImage];
    CGContextDrawImage(context, rect, myCGImage);  //rect is passed to drawRect
    

    【讨论】:

      【解决方案2】:

      应该没有问题。只要确保你正确地保留了它以及你课堂上没有的东西。

      【讨论】:

      • 是的,当您将对象添加到集合中时,集合将保留该对象。当从集合中移除或释放集合时,该对象将被释放。
      • 只需确保在添加到集合之前自动释放您的对象。
      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多