【问题标题】:Low memory waning while taking screenshot of a view in iPhone在 iPhone 中截取视图时出现内存不足警告
【发布时间】:2013-07-15 11:43:30
【问题描述】:

我有一个应用程序,我在其中截取视图并将该图像保存在文档文件夹中。

我正在使用以下代码。

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];

如果我截取 15 到 20 次屏幕截图,它可以正常工作,但之后它会给我低内存警告,然后应用程序在此代码上崩溃。

是否有更优化的代码可以使用,不会导致此类内存问题。

请帮助我。

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch ios6


    【解决方案1】:

    用我下面的方法截屏..

    - (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;
    
    }
    

    看我的另一个答案howe-to-capture-uiview-top-uiview

    【讨论】:

    猜你喜欢
    • 2012-04-02
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多