[英]Objective-c UIImage storage in MSMutableArray alternative, potential memory-leaks?
[英]How do you add an UIImage to a MSMutableArray in Objective-C?
我有新手 objective-c 问题。 如何将 UIImage 添加到 MSMutableArray? 这是我的代码:
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];
但是当它试图添加图像时,这会使程序崩溃。 我在某处读到它添加了一个变量而不是指针,对吗? 如何将 UIImage 添加到数组中,它是 object 不是吗?
谢谢!
您正在初始化数组arr
但将图像添加到(显然未初始化的)数组imageArr
。
根据您更新的代码,我会说image
object 可能是nil
。 文档中明确禁止使用nil
值调用addObject:
。
@thiesdiggity:据我所知,您的代码没有太大问题。 但只要尝试下面的代码,因为我认为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];
}
让我知道您是否需要更多帮助,请发布您的崩溃日志以获得更多帮助
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.