iOS App Crash when received warning memory
iOS App Crash when receiving warning memory
我使用两种方法从 UIImage 获取透明像素。当我在屏幕上画一条线时,他们被调用了很多次。当开始调用这些方法时,应用程序崩溃,并且我收到内存警告。我已经加了@autorelease,还有CGContextFlush(),还是不能work.How解决这个问题?有人知道或遇到过同样的问题吗?
-(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;
}
CGBitmapContextCreateImage()
创建的image
好像没有发布。
你应该在 CGImageRelease()
.
之前发布它
顺便说一句,您不应该释放从 UIGraphicsGetCurrentContext()
检索到的 context
。
CGImageRef image = CGBitmapContextCreateImage(context);
BOOL isTransparent = [self isTransparentPixel:point image:image];
CGImageRelease(image);
return isTransparent;
我使用两种方法从 UIImage 获取透明像素。当我在屏幕上画一条线时,他们被调用了很多次。当开始调用这些方法时,应用程序崩溃,并且我收到内存警告。我已经加了@autorelease,还有CGContextFlush(),还是不能work.How解决这个问题?有人知道或遇到过同样的问题吗?
-(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;
}
CGBitmapContextCreateImage()
创建的image
好像没有发布。
你应该在 CGImageRelease()
.
顺便说一句,您不应该释放从 UIGraphicsGetCurrentContext()
检索到的 context
。
CGImageRef image = CGBitmapContextCreateImage(context);
BOOL isTransparent = [self isTransparentPixel:point image:image];
CGImageRelease(image);
return isTransparent;