UIImage 上下文编辑导致 SIGABRT / EXC_BAD_ACCESS
我的代码有一个非常奇怪的行为。我想在 UIGraphicsImageContext 中编辑一些图形内容。有时有效,有时无效。我将图像处理函数分离到一个新线程,如下所示:
-(void)process:(SEL)function withObject:(id)sender {
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame = CGRectMake(round((self.view.frame.size.width - 50) / 2),
round((self.view.frame.size.height - 50) / 2), 50, 50);
av.tag = kActivityTag;
[self.view addSubview:av];
[av startAnimating];
[self enableControls:NO];
[NSThread detachNewThreadSelector:function toTarget:self withObject:sender];
在该函数之一调用处理函数之前:
-(void)imageColorTintChanged:(id)sender {
[self process:@selector(tint:) withObject:sender];}
-(void)tint:(id)sender {
@synchronized(image) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float f = ((UISlider *)sender).value;
NSInteger tag = ((UISlider *)sender).tag;
if (tag == 0) {
redTint = f;
} else if (tag == 1) {
greenTint = f;
} else if (tag == 2) {
blueTint = f;
}
previewImage = [ImageUtil colorizeImage:image color:[UIColor colorWithRed:redTint green:greenTint blue:blueTint alpha:1.0]];
[imageView setImage:previewImage];
[self processDidFinish];
[pool release];
}}
最后,我的图形编辑在新线程上开始:
+ (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
if (baseImage) {
@synchronized (baseImage) {
UIGraphicsBeginImageContext(baseImage.size); // CRASH!!
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0.0f, 0.0f, baseImage.size.width, baseImage.size.height);
CGContextTranslateCTM(ctx, 0.0, baseImage.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[theColor set];
CGContextFillRect(ctx, area);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, area, baseImage.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
}
return baseImage;
大多数时候它会在此崩溃行:
UIGraphicsBeginImageContext(baseImage.size);
它可能是什么?线程的东西? 提前致谢。
编辑:
嗯,我在 Instruments 上做了一个活动监视器测试,由于某种原因,它说没有内存泄漏,但在一些图像处理后,实际内存使用量上升到 53 MB,然后崩溃。是否有可能这只是一些“我使用了太多内存”错误?
I've got a pretty strange behaviour on my code. I want to edit some graphical stuff within an UIGraphicsImageContext. Sometimes it works, sometimes it doesn't. I'm detaching the image proccessing function to a new thread which looks like this:
-(void)process:(SEL)function withObject:(id)sender {
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame = CGRectMake(round((self.view.frame.size.width - 50) / 2),
round((self.view.frame.size.height - 50) / 2), 50, 50);
av.tag = kActivityTag;
[self.view addSubview:av];
[av startAnimating];
[self enableControls:NO];
[NSThread detachNewThreadSelector:function toTarget:self withObject:sender];
Before the process function is called by one of this functions like that:
-(void)imageColorTintChanged:(id)sender {
[self process:@selector(tint:) withObject:sender];}
-(void)tint:(id)sender {
@synchronized(image) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float f = ((UISlider *)sender).value;
NSInteger tag = ((UISlider *)sender).tag;
if (tag == 0) {
redTint = f;
} else if (tag == 1) {
greenTint = f;
} else if (tag == 2) {
blueTint = f;
}
previewImage = [ImageUtil colorizeImage:image color:[UIColor colorWithRed:redTint green:greenTint blue:blueTint alpha:1.0]];
[imageView setImage:previewImage];
[self processDidFinish];
[pool release];
}}
Finally my graphic editing starts here on the new thread:
+ (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
if (baseImage) {
@synchronized (baseImage) {
UIGraphicsBeginImageContext(baseImage.size); // CRASH!!
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0.0f, 0.0f, baseImage.size.width, baseImage.size.height);
CGContextTranslateCTM(ctx, 0.0, baseImage.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
[theColor set];
CGContextFillRect(ctx, area);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, area, baseImage.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
}
return baseImage;
Most of the time it crashes in this line:
UIGraphicsBeginImageContext(baseImage.size);
What could it possibly be? A threading thing?
Thanks in advance.
EDIT:
Well, i made an activity monitor test on Instruments and for some reason it says no memory leak, but the real Memory usage goes up to 53 MB after some image processing and then it crashes. Is it possible that it's just some "I use too much memory" Error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 UIKit 函数参考,关于
UIGraphicsBeginImageContext()
:您需要在此处创建一个
CGBitmapContext
。From UIKit Function Reference, regarding
UIGraphicsBeginImageContext()
:You'll need to create a
CGBitmapContext
here instead.