简体   繁体   English

在iPhone中拍摄视图的屏幕截图时内存不足

[英]Low memory waning while taking screenshot of a view in iPhone

I am having an app in which I am taking a screenshot of a view and saving that image on documents folder. 我有一个应用,其中我正在查看视图的屏幕截图并将该图像保存在documents文件夹中。

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. 如果我将屏幕截图拍摄15到20次,效果会很好,但是在那之后它会向我发出内存不足警告,并且此代码之后应用程序将崩溃。

Is there a more optimized code that I can use which does not cause such memory problems. 有没有我可以使用的更优化的代码,所以不会引起此类内存问题。

Please help me. 请帮我。

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 看到我的另一个答案howe-to-capture-uiview-top-uiview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM