Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7767483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:41:22+00:00 2026-06-01T15:41:22+00:00

UIImage always become indistinct when it was scaled.What can i do if make it

  • 0

UIImage always become indistinct when it was scaled.What can i do if make it keep clearness?

- (UIImage *)rescaleImageToSize:(CGSize)size {
    CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    [self drawInRect:rect];  // scales image to rect
    UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resImage;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:41:24+00:00Added an answer on June 1, 2026 at 3:41 pm

    Rounding

    First, make sure that you’re rounding your size before scaling. drawInRect: can blur an otherwise usable image in this case. To round to the nearest integer value:

    size.width = truncf(size.width);
    size.height = truncf(size.height);
    

    For certain tasks, you may want to round down (floorf) or round up (ceilf) instead.

    CILanczosScaleTransform not available

    Then, disregard my previous recommendation of CILanczosScaleTransform. While parts of Core Image are available in iOS 5.0, Lanczos scaling is not. If it ever does become available, make use of it. For people working on Mac OS, it is available, use it.

    vImage Scaling

    However, there is a high-quality scaling algorithm available in vImage. The following pictures show how a method using it (vImageScaledImage) compares with the different context interpolation options. Also note how those options behave differently at different zoom levels.

    On this diagram, it preserved the most line detail:
    Scaling comparison on diagram

    On this photograph, compare the leaves at lower left:
    Scaling comparison on tree photograph

    On this photograph, compare the textures in lower right:
    Scaling comparison on rock photograph

    Do not use it on pixel art; it creates odd scaling artifacts:
    Scaling comparison on pixel art, showing scaling artifacts

    Although it on some images it has interesting rounding effects:
    Scaling comparison on Space Invader

    Performance

    Not surprisingly, kCGImageInterpolationHigh is the slowest standard image interpolation option. vImageScaledImage, as implemented here, is slower still. For shrinking the fractal image to half its original size, it took 110% of the time of UIImageInterpolationHigh. For shrinking to a quarter, it took 340% of the time.

    You may think otherwise if you run it in the simulator; there, it can be much faster than kCGImageInterpolationHigh. Presumably the vImage multi-core optimisations give it a relative edge on the desktop.

    Code

    // Method: vImageScaledImage:(UIImage*) sourceImage withSize:(CGSize) destSize
    // Returns even better scaling than drawing to a context with kCGInterpolationHigh.
    // This employs the vImage routines in Accelerate.framework.
    // For more information about vImage, see https://developer.apple.com/library/mac/#documentation/performance/Conceptual/vImage/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001001-CH201-TPXREF101
    // Large quantities of memory are manually allocated and (hopefully) freed here.  Test your application for leaks before and after using this method.
    - (UIImage*) vImageScaledImage:(UIImage*) sourceImage withSize:(CGSize) destSize;
    {
        UIImage *destImage = nil;
    
        if (sourceImage)
        {
            // First, convert the UIImage to an array of bytes, in the format expected by vImage.
            // Thanks: http://stackoverflow.com/a/1262893/1318452
            CGImageRef sourceRef = [sourceImage CGImage];
            NSUInteger sourceWidth = CGImageGetWidth(sourceRef);
            NSUInteger sourceHeight = CGImageGetHeight(sourceRef);
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            unsigned char *sourceData = (unsigned char*) calloc(sourceHeight * sourceWidth * 4, sizeof(unsigned char));
            NSUInteger bytesPerPixel = 4;
            NSUInteger sourceBytesPerRow = bytesPerPixel * sourceWidth;
            NSUInteger bitsPerComponent = 8;
            CGContextRef context = CGBitmapContextCreate(sourceData, sourceWidth, sourceHeight,
                                                         bitsPerComponent, sourceBytesPerRow, colorSpace,
                                                         kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
            CGContextDrawImage(context, CGRectMake(0, 0, sourceWidth, sourceHeight), sourceRef);
            CGContextRelease(context);
    
            // We now have the source data.  Construct a pixel array
            NSUInteger destWidth = (NSUInteger) destSize.width;
            NSUInteger destHeight = (NSUInteger) destSize.height;
            NSUInteger destBytesPerRow = bytesPerPixel * destWidth;
            unsigned char *destData = (unsigned char*) calloc(destHeight * destWidth * 4, sizeof(unsigned char));
    
            // Now create vImage structures for the two pixel arrays.
            // Thanks: https://github.com/dhoerl/PhotoScrollerNetwork
            vImage_Buffer src = {
                .data = sourceData,
                .height = sourceHeight,
                .width = sourceWidth,
                .rowBytes = sourceBytesPerRow
            };
    
            vImage_Buffer dest = {
                .data = destData,
                .height = destHeight,
                .width = destWidth,
                .rowBytes = destBytesPerRow
            };
    
            // Carry out the scaling.
            vImage_Error err = vImageScale_ARGB8888 (
                                                     &src,
                                                     &dest,
                                                     NULL,
                                                     kvImageHighQualityResampling 
                                                     );
    
            // The source bytes are no longer needed.
            free(sourceData);
    
            // Convert the destination bytes to a UIImage.
            CGContextRef destContext = CGBitmapContextCreate(destData, destWidth, destHeight,
                                                             bitsPerComponent, destBytesPerRow, colorSpace,
                                                             kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
            CGImageRef destRef = CGBitmapContextCreateImage(destContext);
    
            // Store the result.
            destImage = [UIImage imageWithCGImage:destRef];
    
            // Free up the remaining memory.
            CGImageRelease(destRef);
    
            CGColorSpaceRelease(colorSpace);
            CGContextRelease(destContext);
    
            // The destination bytes are no longer needed.
            free(destData);
    
            if (err != kvImageNoError)
            {
                NSString *errorReason = [NSString stringWithFormat:@"vImageScale returned error code %d", err];
                NSDictionary *errorInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                           sourceImage, @"sourceImage", 
                                           [NSValue valueWithCGSize:destSize], @"destSize",
                                           nil];
    
                NSException *exception = [NSException exceptionWithName:@"HighQualityImageScalingFailureException" reason:errorReason userInfo:errorInfo];
    
                @throw exception;
            }
        }
        return destImage;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Objective: take a UIImage, crop out a square in the middle, change size of
My code is -(UIImage *)addText:(UIImage *)img text:(NSString *)text1 { int w = img.size.width; int
It's really a pain, but always when I draw an UIImage in -drawRect:, it's
I've made this below fitImage function which takes an UIImage and a CGSize .
why can´t I fill my NSArray? Where is my mistake? He always just fill
Root view is a UIImage View, it has subviews, those have subviews. My root
Given a UIImage of any dimension, I wish to generate a square icon sized
If I use [UIImage imageWithCGImage:] , passing in a CGImageRef , do I then
I have a UIImage containing an image with a whole bunch of smaller pictures
According to the UIImage documentation : In low-memory situations, image data may be purged

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.