I have UIImageView, which is used for showing image from resources. Image appears to be distorted. It is especially visible if you zoom the image in a graphics editor.
UIImageView size matches the size of the image.
currently used UIImageView parameters.
Code:
imgHeader.contentMode = UIViewContentModeCenter;
imgHeader.image = [UIImage imageNamed:@"logo.png"];
imgHeader.opaque = TRUE;
imgHeader.clearsContextBeforeDrawing = TRUE;
For UIImageView I tried changing parameters "opaque", "clearsContextBeforeDrawing" and "contentMode". Same result.
For resolving this issue I have also wrote classes AliasedLayer and AliasedImageView ( for antialiasing disabling the use of CGContextSetShouldAntialias and CGContextSetAllowsAntialiasing )
This is approximate code, there have been many attempts to change the code to correct image distortion. Same result.
Code:
@implementation AliasedLayer
-(id)init
{
self = [super init];
if (self) {
img = [UIImage imageNamed:@"logo.png"];
}
return self;
}
-(void)drawInContext:(CGContextRef)context
{
CGContextSetAllowsAntialiasing(context, false);
CGContextSetShouldAntialias(context, false);
CGContextDrawImage(context, CGRectMake(0., 0., 55., 18.), [img CGImage]);
}
- (void)renderInContext:(CGContextRef)ctx
{
CGContextSetAllowsAntialiasing(ctx, false);
CGContextSetShouldAntialias(ctx, false);
}
@end
@implementation AliasedImageView
- (id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setClearsContextBeforeDrawing:YES];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void)setImage:(UIImage *)img
{
[image release];
image = img;
[image retain];
}
- (void)setNeedsDisplay{
[self setNeedsDisplayInRect:self.frame];
}
-(void)dealloc
{
[image release];
[super dealloc];
}
+(Class)layerClass
{
return [AliasedLayer class];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextSetAllowsAntialiasing(ctx, false);
CGContextSetShouldAntialias(ctx, false);
CGContextDrawImage(ctx, rect, [image CGImage]);
}
-(void)awakeFromNib
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(ctx, false);
CGContextSetShouldAntialias(ctx, false);
[super awakeFromNib];
}
@end
There have been attempts to dynamically load the image as well as using xib file. Same result - logo image is distorted.
Any ideas on what is causing this and how to fix it?