Showing posts with label UIImage. Show all posts
Showing posts with label UIImage. Show all posts

Wednesday, 17 July 2013

Resize UImage

UIImage can be re sized or scaled to fit in frame or to reduce size to send over network. Below is the utility function that takes UIImage and target dimensions (in terms of CGSize), and returns resized UIImage.

---------------------------------------------------------------------------
Example - 1
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
---------------------------------------------------------------------------
Example - 2
UIGraphicsBeginImageContext(CGSizeMake(100,100));
 [imgView.image drawInRect:CGRectMake(0, 0,100,100)];
 UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 imgView.image = scaledImage;
 UIGraphicsEndImageContext();
---------------------------------------------------------------------------
Include QuartzCore.FrameWork in project
Import <QuartzCore/QuartzCore.h> in implementation file

Example - 3
- (UIImage *)resizeImageToSize:(CGSize)targetSize
{
    UIImage *sourceImage = self.imageToFitInBackground;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor < heightFactor)
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // make image center aligned
        if (widthFactor < heightFactor)
        {
           thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor > heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if(newImage == nil)
       NSLog(@"could not scale image");
    return newImage ;
}

 ---------------------------------------------------------------------------
Example - 4

- (UIImage *)scaleAndRotateImage:(UIImage *)image
{
    static int kMaxResolution = 300;
   
    CGImageRef imgRef = image.CGImage;
    CGFloat widtha = CGImageGetWidth(imgRef);
    CGFloat heighta = CGImageGetHeight(imgRef);
   
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, widtha, heighta);
    if (widtha > kMaxResolution || heighta > kMaxResolution) {
        CGFloat ratio = widtha/heighta;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        } else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }
   
    CGFloat scaleRatio = bounds.size.width / widtha;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
   
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {
        case UIImageOrientationUp:
            transform = CGAffineTransformIdentity;
            break;
        case UIImageOrientationUpMirrored:
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
        case UIImageOrientationDown:
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
        case UIImageOrientationLeftMirrored:
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationLeft:
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationRightMirrored:
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        case UIImageOrientationRight:
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    }
   
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -heighta, 0);
    } else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -heighta);
    }
    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, widtha, heighta), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
   
    UIGraphicsBeginImageContext(CGSizeMake(250.0f,250.0f));
    [imageCopy drawInRect:CGRectMake(0, 0, 250, 250)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();   
    UIGraphicsEndImageContext();
    // return newImage;
   
   
    return newImage;
}




UIImage Utility 
UIImage 

Friday, 21 June 2013

UIImage - Make Image Blur

In iOs 7 there is grate use of blur image. Its look awesome .. Here is function to make any image blur. In that original image is blur start with 0.0 and increase it unto 1.0 as requirement.

Example :

- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if ((blur < 0.0f) || (blur > 1.0f)) {
        blur = 0.5f;
    }
   
    int boxSize1 = (int)(blur * 100);
    boxSize1 -= (boxSize1 % 2) + 1;
   
    CGImageRef img = image.CGImage;
   
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    void *pixelBuffer;
   
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
   
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
   
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
   
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
       
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
   
    error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL,
                                       0, 0, boxSize1, boxSize1, NULL,
                                       kvImageEdgeExtend);
   
   
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
   
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             CGImageGetBitmapInfo(image.CGImage));
   
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
   
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
   
    free(pixelBuffer);
    CFRelease(inBitmapData);
   
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
   
    return returnImage;
}



Hope this will useful in your code…. if any useful code for same please share it with us…..Thank You…:)