UIImage 上下文编辑导致 SIGABRT / EXC_BAD_ACCESS

发布于 2024-11-27 07:28:34 字数 2427 浏览 1 评论 0原文

我的代码有一个非常奇怪的行为。我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2024-12-04 07:28:34

来自 UIKit 函数参考,关于UIGraphicsBeginImageContext()

您应该仅从应用程序的主线程调用此函数。

您需要在此处创建一个CGBitmapContext

From UIKit Function Reference, regarding UIGraphicsBeginImageContext():

You should call this function from the main thread of your application only.

You'll need to create a CGBitmapContext here instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文