0

我正在通过以下代码截取我上次查看的屏幕的屏幕截图

//Image Code  
UIImage *nextImage;  
CGImageRef imgRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());  
nextImage = [UIImage imageWithCGImage:imgRef];  
CGImageRelease(imgRef);  
CGContextRelease(UIGraphicsGetCurrentContext());   

当我在控制台中显示图像时,我得到了 O/P,如: UIImage:0x929c760
问题就在这里,当我将该 UIImage 转换为 NSData 时,我得到了空值。

我尝试过的事情是

//tried    
NSData *data = UIImagePNGRepresentation(nextImage);  
NSData *data2 = UIImageJPEGRepresentation(nextImage, 1.0);
4

1 回答 1

2

大家好,我已经得到了@NP Compete链接给出的我的问题的解决方案//getting screenshot我没有使用任何UIImageview所以我的代码如下

-(UIImage*)doImageOperation
{
    UIImage *image = [[UIImage alloc]init];
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"image in cg %@",image);
    [self saveImage:image];
    return image;
}

// 保存到文档目录

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            path = [documentsDirectory stringByAppendingPathComponent: 
                    [NSString stringWithString:@"test.png"]];
 data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
NSData *data = UIImagePNGRepresentation(image1);
        NSLog(@"data %@",data);
于 2012-04-20T05:09:32.717 回答