English 中文(简体)
无法将图像保存在 NSMEMEM TableAray 中
原标题:unable to store an image in an NSMutableArray

我试图在NSMMetable 阵列中保存图像,但无法工作

这就是我正在做的

[imagesList addObject:[UIImage imageNamed:@"b.png"]];

after executing this line I noticed that the number of objects remains 0 any reason ?

谢谢 谢谢

我在若干领域重复这一守则:

在全球范围内,我宣布:

NSMutableArray *imagesList;
NSUserDefaults *imagesHistory;

在我的观点中,我写道:

imagesHistory=[NSUserDefaults standardUserDefaults];  //imagesHistory is created globallt as NSUserDefault imagesHistory
[imagesHistory setObject:imagesList forKey:@"images history"];

UIImage *image;
image=[UIImage imageNamed:@"b.png"];

[imagesList addObject:image];
imagesHistory=[NSUserDefaults standardUserDefaults];
[imagesHistory setObject:imagesList forKey:@"images history"];

我写道:(即使我不需要在添加弦的时候这样做...)

 imagesList = [[NSMutableArray alloc] init];
问题回答

无论是否为全球变量,您仍然需要为对象调用 alloc init OCEWHERE。 如果您打算在整个应用程序中使用, 那么 appted- finish Launching withouts 是添加此调用的适当地方 :

imagesList = [[NSMutableArray alloc] init];

您的空数组是否被保留? 如果您不使用自动引用计数, 很有可能您会以下列方式重新初始化数组

imagesList = [[NSMutableArray alloc] init];

但它没有被保留。 您会想要保留空数组, 这样它就会被附加到您的代码中 。

imagesList = [[[NSMutableArray alloc] init] retain];

请不要忘记当您在 view didnload 中或适当时, 在全部完成后释放数组 。

您重新配置/ 启用 < code> imagesList < / code> > 之后您试图添加一个对象。 您需要在添加任何内容前先配置 Aloc/ init < code> imageList

为了确保它在那里, 尝试这样的东西:

if (!imagelist) 图像列表 = [[NSmuttableArraray alloc] init]; (!imagelist) 图像列表 = [[NSmuttable Arraray alloc] init];

如果存在, 而且您仍然有这个问题, 您有可能重新配置/ 启用一个新的 NSMableArray, 在您添加了对象后, 将其分配到 < code> imageList 。 这意味着旧的数组将被丢弃, 而您将会被留下一个带有零项的新数组 。

try UIImageView *image; image=[UIImage imageNamed:@"b.png"];

另外,在中,使用强力而不是保留





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签