Organizational Research By

Surprising Reserch Topic

howe to capture uiview top uiview


howe to capture uiview top uiview  using -'iphone,ios,uiview,uiimage'

i have UIView which displays the Graph..now I need to capture it to the UIImage and I googled it and got the below code.but if i use this in my code its not work.even if I use breakpoint compiler does not reach to this.I am using this in my UIView .where I am going wrong?

+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
}

    

asked Sep 22, 2015 by mca.agarwal
0 votes
12 views



Related Hot Questions

3 Answers

0 votes

try my this method and code ..

- (UIImage *)captureView {

//hide controls if needed
CGRect rect = [self.view bounds];

    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

And call it like bellow...

UIImage *tempImageSave=[self captureView];

and you can also save this image with this bellow line for photo album..

UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);

and you can also save this image with this bellow line for Document Directory..

            NSData *imageData = UIImagePNGRepresentation(tempImageSave);
            NSFileManager *fileMan = [NSFileManager defaultManager];

            NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
            [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

i hope this helpful for you..

answered Sep 22, 2015 by loknath.ganji
0 votes

Try with this code it may be help you

- (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}
answered Sep 22, 2015 by akhilesh
0 votes

you can save image of View like this way and store image in to Document Directory:-

-(void)SaveViewAsImage
{
    UIGraphicsBeginImageContext(self.view.bounds.size);

        UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIGraphicsEndImageContext();
        NSData *imageData = UIImagePNGRepresentation(saveImage);
        NSFileManager *fileMan = [NSFileManager defaultManager];

        NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
        [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];

}

Your image save in Document Directory :)

Download this demo

http://www.sendspace.com/file/gjxa82

answered Sep 22, 2015 by vimaldas2005

...