简体   繁体   中英

How do you add an UIImage to a MSMutableArray in Objective-C?

I have noob objective-c question. How do you add an UIImage to a MSMutableArray? Here is the code that I have:

MSMutableArray *arr = [[NSMutableArray alloc] init];
UIImage *image;
UIGraphicsBeginImageContext(contextSize);
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, pushUpSize);

    [screenWindow.layer renderInContext: context];  
    image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
[arr addObject:image];

But this crashes the program when it tries to add the image. I read somewhere that its adding a variable but not the pointer, is that correct? How do I add the UIImage to the array, its an object isn't it?

Thanks!

You are initializing the array arr but adding the images to the (apparently unitialized) array imageArr .

Based on your updated code, I would say that the image object is probably nil . Calling addObject: with a nil value is specifically prohibited in the docs .

@thiesdiggity: Not much is wrong in your code from what I can see. But just try out the below code as I think the image object is nil :

MSMutableArray *arr = [[NSMutableArray alloc] init];
UIImage *image;
UIGraphicsBeginImageContext(contextSize);
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, pushUpSize);

    [screenWindow.layer renderInContext: context];  
    image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();

if(image != nil)
{
    [arr addObject:image];
}

Let me know if you need more help and please post your crash log for more help

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM