Powered By Blogger

Monday 10 December 2012

Image resizing and masking in IOS



Resize UIImage :

+(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)destSize{
    float currentHeight = image.size.height;
    float currentWidth = image.size.width;
    float liChange ;
    CGSize newSize ;
    if(currentWidth == currentHeight) // image is square
        {
             liChange = destSize.height / currentHeight;
             newSize.height = currentHeight * liChange;
             newSize.width = currentWidth * liChange;
            }
    else if(currentHeight > currentWidth) // image is landscape
         {
              liChange = destSize.width / currentWidth;
              newSize.height = currentHeight * liChange;
              newSize.width = destSize.width;
             }
    else        // image is Portrait
         {
             liChange = destSize.height / currentHeight;
             newSize.height= destSize.height;
             newSize.width = currentWidth * liChange;
             }
    
    
    UIGraphicsBeginImageContext( newSize );
    CGContextRef    context;
    UIImage      *outputImage = nil;
    
    context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    [image drawInRect:CGRectMake( 0, 0, newSize.width, newSize.height )];
    outputImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef;
    int x = (newSize.width == destSize.width) ? 0 : (newSize.width - destSize.width)/2;
    int y = (newSize.height == destSize.height) ? 0 : (newSize.height - destSize.height )/2;
    if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, CGRectMake(x, y, destSize.width, destSize.height) ) ) ) {
         outputImage = [[UIImage alloc] initWithCGImage: imageRef] ;
         CGImageRelease( imageRef );
        }
     
    return outputImage;
}


Mask image:

+(UIImage *)maskImage:(UIImage *)image andMaskingImage:(UIImage *)maskingImage{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGImageRef maskImageRef = [maskingImage CGImage];
    CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskingImage.size.width, maskingImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    if (mainViewContentContext==NULL)
         return NULL;
    CGFloat ratio = 0;
    ratio = maskingImage.size.width/ image.size.width;
    if(ratio * image.size.height < maskingImage.size.height) {
         ratio = maskingImage.size.height/ image.size.height;
        }
    CGRect rect1= {{0, 0}, {maskingImage.size.width, maskingImage.size.height}};
    CGRect rect2= {{-((image.size.width*ratio)-maskingImage.size.width)/2 , -((image.size.height*ratio)-maskingImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};
    CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
    CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);
    CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
    CGContextRelease(mainViewContentContext);
    UIImage *theImage = [UIImage imageWithCGImage:newImage];
    CGImageRelease(newImage);
     // return the image
    return theImage;
}

More details please visit following link

http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html

No comments:

Post a Comment