在iphone中以高分辨率将图像保存在文件夹中

时间:2011-09-27 11:58:23

标签: iphone uiimage

我想以高分辨率保存在文档文件夹中的iphone屏幕上绘制的图像。在这里,我可以将图像保存在文档文件夹中,但分辨率非常低。 任何人都可以告诉我如何完成这项任务。

这是我的代码

    UIImage *image = [[UIImage alloc]init];
double resolution = 50;

    //CGSize pageSize = CGSizeMake(612, 792);
    // CGRect boundsRect = CGRectMake(50, 50, 512, 692);
CGRect boundsRect = CGRectMake(0, 0, 612, 792);

double imageWidth = image.size.width * image.scale * 72 / resolution;
double imageHeight = image.size.height * image.scale * 72 / resolution;

double sx = imageWidth / boundsRect.size.width;
double sy = imageHeight / boundsRect.size.height;

    // At least one image edge is larger than maxBoundsRect
if ((sx > 1) || (sy > 1)) {
    double maxScale = sx > sy ? sx : sy;
    imageWidth = imageWidth / maxScale;
    imageHeight = imageHeight / maxScale;
}

    // Put the image in the top left corner of the bounding rectangle
CGRect bounds = CGRectMake(boundsRect.origin.x, boundsRect.origin.y + boundsRect.size.height - imageHeight, imageWidth, imageHeight);

CGSize size = bounds.size;

    //  UIGraphicsBeginImageContextWithOptions(size,NO,10.0f); 

UIGraphicsBeginImageContext(size);
{
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    image = UIGraphicsGetImageFromCurrentImageContext();

}

1 个答案:

答案 0 :(得分:1)

试试这个:

- (UIImage *)screenshotImage {
    UIView* screen = self.view;
    CGSize imageSize = screen.bounds.size;

    UIGraphicsBeginImageContext(imageSize);
    CGContextRef imageContext = UIGraphicsGetCurrentContext();
    [screen.layer renderInContext: imageContext];
    UIImage* screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return screenshotImage;
}

收到图像并保存后:

UIImage *screenshotImage = [self screenshotImage];
UIImageWriteToSavedPhotosAlbum(screenshotImage,nil,nil,nil);