任何iPhone集合对象都可以保存图像吗?

任何iPhone集合对象都可以保存图像吗?,iphone,cocoa-touch,Iphone,Cocoa Touch,实际上,我想要一个包含2个图像对象和1个文本对象的自定义单元格,我决定为这些对象创建一个容器 那么,是否可以在对象中保存图像并将该对象插入任何集合对象中,然后使用该对象在单元格内显示?这应该没有问题。只要确保你正确地保留了它,以及你班上没有的东西。这应该没有问题。只需确保您正确地保留了它以及类中没有的内容。NSArray和NSDictionary都保存了对象。这些最有可能是将用于表视图的集合 实现您要做的事情的最佳方法是使用UIImage类。UIImages包装一个CGImage并为您执行所有内

实际上,我想要一个包含2个图像对象和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

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

是,将对象添加到集合时,集合将保留该对象。从集合中删除或取消分配集合时,对象将被释放。只需确保在添加到集合之前自动释放对象。是的,向集合中添加对象时,集合将保留该对象。从集合中删除对象或取消分配集合时,对象将被释放。只需确保在添加到集合之前自动释放对象即可。