Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create thumbnails from a UIImage

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 244
    Comment on it

    Hi there, often programmers are required to display a large number of images on a a single screen, for example in a photo gallery. Displaying a group of images in their full size can be a bit tricky because it will take a lot of space and the the images will take a lot of time to load because of the size of the images. This in turn will slow down the iPhone's performance. So, to solve this problem the programmer needs top display the images in thumbnails, which will save the memory and will enhance the performance of the iPhone.

    Therefore here's the code that converts the full size image into a small thumbnail whose size can be determined by the programmer according to the need if the app.

    - (UIImage *)thumbWithSideOfLength:(float)length 
            {
            NSString *subdir = @my/images/directory;
            NSString *filename = @myOriginalImage.png;
            NSString *fullPathToThumbImage = [subdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%dx%d%@",(int) length, (int) length,filename];
            NSString *fullPathToMainImage = [subdir stringByAppendingPathComponent:filename];
            UIImage *thumbnail;
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:fullPathToThumbImage] == YES) 
            {
            thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];
            }
            else 
            {
            //couldnt find a previously created thumb image so create one first
            UIImage *mainImage = [UIImage imageWithContentsOfFile:fullPathToMainImage];
            UIImageView *mainImageView = [[UIImageView alloc] initWithImage:mainImage];
            BOOL widthGreaterThanHeight = (mainImage.size.width > mainImage.size.height);
            float sideFull = (widthGreaterThanHeight) ? mainImage.size.height : mainImage.size.width;
            CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);
            //creating a square context the size of the final image which we will then
            // manipulate and transform before drawing in the original image
            UIGraphicsBeginImageContext(CGSizeMake(length, length));
            CGContextRef currentContext = UIGraphicsGetCurrentContext();
            CGContextClipToRect( currentContext, clippedRect);
            CGFloat scaleFactor = length/sideFull;
            if (widthGreaterThanHeight) 
            {
            //a landscape image  make context shift the original image to the left when drawn into the context
            CGContextTranslateCTM(currentContext, -((mainImage.size.width  sideFull) / 2) * scaleFactor, 0);
            }
            else 
            {
            //a portfolio image  make context shift the original image upwards when drawn into the context
            CGContextTranslateCTM(currentContext, 0, -((mainImage.size.height  sideFull) / 2) * scaleFactor);
            }
            //this will automatically scale any CGImage down/up to the required thumbnail side (length) when the GImage gets drawn into the context on the next line of code
            CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);
            [mainImageView.layer renderInContext:currentContext];
            thumbnail = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            NSData *imageData = UIImagePNGRepresentation(thumbnail);
            [imageData writeToFile:fullPathToThumbImage atomically:YES];
            thumbnail = [UIImage imageWithContentsOfFile:fullPathToThumbImage];
            }
            return thumbnail;
            }

    The above given code will provide the perfect replica of the image in a small size and is suitable to use to populate arrays that are used to display the contents of the photo gallery or any other app that has to display a large number of images in a single screen.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: