2013-07-17 23 views
1

我有一个应用程序,我正在截取视图并将该图像保存到文档文件夹中。内存减少,同时在iPhone中截图视图

我正在使用以下代码。

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倍它工作正常,但在那之后它给我的低内存警告,之后在此代码的应用程序崩溃。

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

请帮帮我。

回答

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

+0

感谢您reply.This会给我一个视图的全屏幕截图,如何裁剪视图的特定部分?从上面的代码中我可以看到,我只想采用部分视图。 – Manthan

+0

只是设置框架或矩形而不是'[self.view界限];' –

+0

请帮助我,如果你有任何想法** http://stackoverflow.com/questions/17745058/ftp-downloading-very-慢进-iphone **。 @Paras Joshi – Manthan