简体   繁体   English

如何将 UIImage 添加到 Objective-C 中的 MSMutableArray?

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

I have noob objective-c question.我有新手 objective-c 问题。 How do you add an UIImage to a MSMutableArray?如何将 UIImage 添加到 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?如何将 UIImage 添加到数组中,它是 object 不是吗?

Thanks!谢谢!

You are initializing the array arr but adding the images to the (apparently unitialized) array imageArr .您正在初始化数组arr但将图像添加到(显然未初始化的)数组imageArr

Based on your updated code, I would say that the image object is probably nil .根据您更新的代码,我会说image object 可能是nil Calling addObject: with a nil value is specifically prohibited in the docs . 文档中明确禁止使用nil值调用addObject:

@thiesdiggity: Not much is wrong in your code from what I can see. @thiesdiggity:据我所知,您的代码没有太大问题。 But just try out the below code as I think the image object is nil :但只要尝试下面的代码,因为我认为image object 是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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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