我使用两种方法从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;
}
答案 0 :(得分:0)
似乎image
创建的CGBitmapContextCreateImage()
未发布。
您应该CGImageRelease()
发布它。
顺便说一下,您不应该发布从context
检索到的UIGraphicsGetCurrentContext()
。
CGImageRef image = CGBitmapContextCreateImage(context);
BOOL isTransparent = [self isTransparentPixel:point image:image];
CGImageRelease(image);
return isTransparent;