简体   繁体   中英

Objective-c UIImage bad image quality

I am not the best graphic designer, but need to setup few icons. I made them with gimp with options like Border, etc. All are primary designed in 1000x1000px and used in objective-c code in UIImageView .

I am wondering why resized icon look that terrible. Any suggestions?

In app: http://s14.postimg.org/k2g9uayld/Screen_Shot_2014_12_18_at_11_22_53.png http://s14.postimg.org/biwvwjq8x/Screen_Shot_2014_12_18_at_11_23_02.png

Original image:

Can't Post more than 2 links so: s17.postimg.org/qgi4p80an/fav.png

I dont think that matters but

UIImage *image = [UIImage imageNamed:@"fav.png"];
image = [UIImage imageWithCGImage:[image CGImage] scale:25 orientation:UIImageOrientationUp];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];

But one of images has been set up in storyboard and effect is the same.

    [self.favO.layer setMinificationFilter:kCAFilterTrilinear];
    [self.favO setImage:[self resizeImage:[UIImage imageNamed:@"fav.png"] newSize:CGSizeMake(35,35)]
                        forState:UIControlStateNormal];

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);
    CGContextDrawImage(context, newRect, imageRef);

    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();

    return newImage;
}

This fixed problem. This problem occurs while UIImageView takes care of resizing.

To avoid this kind of behavior I suggest you make SVGs instead of png files. Using the lib SVGKit you can then resize it to your heart content. Since SVG format is a vectorial format there won't be any loss in your scaling. https://github.com/SVGKit/SVGKit

EDIT: To add up to this solution, your PNG file at 1000X1000px must be really heavy, on the other hand a SVG file is a text file so it makes it very lightweight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM