我使用两种方法从UIImage获取透明像素。在屏幕上画线时,它们被多次调用。当方法开始被调用时,应用程序崩溃,并且我收到内存警告。我已经添加了@autorelease和CGContextFlush(),但是它仍然不起作用。如何解决这个问题?有谁知道或遇到过同样的问题?

-(BOOL)isTransparency:(CGPoint)point {
    
    @autoreleasepool {
        
        UIGraphicsBeginImageContext(self.imageView.superview.bounds.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        CGContextTranslateCTM(context, [self.imageView center].x, [self.imageView center].y);
        CGContextConcatCTM(context, [self.imageView transform]);
        CGContextTranslateCTM(context,
                              -[self.imageView bounds].size.width * [[self.imageView layer] anchorPoint].x,
                              -[self.imageView bounds].size.height * [[self.imageView layer] anchorPoint].y);
        
        [[self.imageView image] drawInRect:[self.imageView bounds]];
        
        CGContextRestoreGState(context);
        
        CGImageRef image = CGBitmapContextCreateImage(context);
        
        
        CGContextRelease(context);
       
        return [self isTransparentPixel:point image:image];
    }
}

-(BOOL)isTransparentPixel:(CGPoint)point image:(CGImageRef)cgim{
        
        unsigned char pixel[1] = {0};
        
        CGContextRef context = CGBitmapContextCreate(pixel,
                                                     1, 1, 8, 1, NULL,
                                                     kCGImageAlphaOnly);
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        CGContextDrawImage(context, CGRectMake(-point.x,
                                               -(self.imageView.superview.frame.size.height - point.y),
                                               CGImageGetWidth(cgim),
                                               CGImageGetHeight(cgim)),cgim);
        
        CGContextRelease(context);
        
        CGFloat alpha = pixel[0]/255.0;
      
        return alpha < 0.01;
    
    
}

最佳答案

似乎没有发布由image创建的CGBitmapContextCreateImage()
您应该通过CGImageRelease()释放它。

顺便说一句,您不应释放从context检索到的UIGraphicsGetCurrentContext()

CGImageRef image = CGBitmapContextCreateImage(context);

BOOL isTransparent = [self isTransparentPixel:point image:image];
CGImageRelease(image);
return isTransparent;

关于iphone - 收到警告内存时,iOS App崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28951577/

10-17 01:26