Wednesday, January 30, 2013

Take Bitmap from UIView

You can capture UIImage from any UIView by following function

*Note: Import the following class to use UIView layer properties 
 #import <QuartzCore/QuartzCore.h>

Code is :

+ (UIImage*)imageFromView:(UIView*)view
    [self beginImageContextWithSize:[view bounds].size];
    BOOL hidden = [view isHidden];
    [view setHidden:NO];
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    [view setHidden:hidden];
    return image;

+ (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
    UIImage *image = [self imageFromView:view];
    if ([view bounds].size.width != newSize.width ||
        [view bounds].size.height != newSize.height) {
        image = [self imageWithImage:image scaledToSize:newSize];
    return image;

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    [self beginImageContextWithSize:newSize];
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    [self endImageContext];
    return newImage;

Above Reference is taken from :

No comments:

Post a Comment