1

I am having an app in which I am taking a screenshot of a view and saving that image on documents folder.

I am using the following code.

CGSize size = self.view.bounds.size;
    CGRect cropRect;
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if([self isPad])
    {
        cropRect = CGRectMake(145, 110, 476, 476);
    }
    else
    {
        if (screenBounds.size.height ==568)
        {

            cropRect = CGRectMake(40, 69, 240, 240);
        }

        else
        {
            cropRect = CGRectMake(40, 62, 240, 240);
        }
    }

    /* Get the entire on screen map as Image */
    UIGraphicsBeginImageContext(size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    /* Crop the desired region */
    CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
    UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    /* Save the cropped image
     UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);*/

    //save to document folder
    NSData * imageData = UIImageJPEGRepresentation(cropImage, 1.0);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

        imagename=[NSString stringWithFormat:@"Fff.jpg"];

    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imagename];
    ////NSLog(@"full path %@",fullPathToFile);
    [imageData writeToFile:fullPathToFile atomically:NO];

It works fine if I take the screenshot 15 to 20 times but after that It gives me low memory warning and the app crashes after that on this code.

Is there a more optimized code that I can use which does not cause such memory problems.

Please help me.

Manthan
  • 3,856
  • 1
  • 27
  • 58

1 Answers1

1

Capture screen with my bellow method..

- (UIImage *)captureView {

    //hide controls if needed
    CGRect rect = [self.view bounds];// Here define CGRect with your requirement of take screenshot of some part

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

}

See my another answer howe-to-capture-uiview-top-uiview

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Thanks for your reply.This will give me full screenshot of a view but how to crop specific part of a view? I want to take only some part of a view as you can see from my code above. – Manthan Jul 17 '13 at 05:33
  • for that just set frame or rect instead of `[self.view bounds];` – Paras Joshi Jul 17 '13 at 05:34
  • Please help me on this if you have any idea **http://stackoverflow.com/questions/17745058/ftp-downloading-very-slow-in-iphone**. @Paras Joshi – Manthan Jul 19 '13 at 11:53
  • :can you help me with http://stackoverflow.com/questions/18355299/how-to-make-gridview-like-aweditorium-ipad-app-like-scrolling-infinitely?noredirect=1#comment26946881_18355299 – Manthan Aug 21 '13 at 11:02
  • Can you please help me with this http://stackoverflow.com/questions/19108185/lag-while-drawing-in-ios7 @Paras Joshi. – Manthan Oct 01 '13 at 11:02
  • :Can you please help me with this http://stackoverflow.com/questions/26277592/upload-image-on-webserver-through-json-in-ios-using-php – Manthan Oct 10 '14 at 04:46
  • @Manthan i will look at that when ii have some time dude... sorry for late reply but i'll give answer when i have time.. now some busy schedule dude so... – Paras Joshi Oct 10 '14 at 05:18
  • Yes, I can understand man. No worries. I'll check it and let u know. – Manthan Oct 10 '14 at 05:34
  • If you have time please help me. I am stuck at one point very badly. – Manthan Oct 11 '14 at 10:44