繁体   English   中英

如何保存缩放后的uiimage

[英]How to save a scaled uiimage

屏幕图像链接savePic图像链接 ,您可以找到screenImg和savePic之间的区别。 我将backgroundImageView设置为一个非常小的png。 我想将backgroundimageview保存为图片。在iPhone Simulator屏幕中,图像的边缘正常,但是savePic的边缘不清晰。 任何人都可以告诉我如何保存高清图片。

- (void)viewDidLoad
{
    [super viewDidLoad];
    //_backImgView 320*480
    self.backImgView.image = [UIImage imageNamed:@"Purple.png"];

    [self performSelector:@selector(savePic) withObject:nil afterDelay:0.1];
}

- (void)savePic
{
    BOOL isDir;
    NSString *dirPath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/Pic"]; 
    NSFileManager *fm = [NSFileManager defaultManager];
    if(![fm fileExistsAtPath:dirPath isDirectory:&isDir]){
        BOOL bo = [fm createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
        if(!bo){
            NSLog(@"-->Create CardSlide Dir Fail!!!");
            return;
        }
    }

    UIGraphicsBeginImageContext(_backImgView.bounds.size);
    //UIGraphicsBeginImageContextWithOptions(_backImgView.bounds.size, _backImgView.opaque, 2.0);
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
    [_backImgView drawRect:_backImgView.bounds];
    //[_backImgView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *bgImg = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();

    NSString *path = [NSString stringWithFormat:@"save_%@.jpg", @"demo"];
    NSString *jpgPath = [dirPath stringByAppendingPathComponent:path]; 
    BOOL isSucc = [UIImageJPEGRepresentation(bgImg, 1.0) writeToFile:jpgPath atomically:YES];
    NSLog(@"%@ write photo %@!", jpgPath, isSucc?@"SUCC":@"FAIL");
}

使用小图像,不可能以正确的方式将其缩放到更大的图像,您将始终经历模糊,嘈杂的图像,但是如果图像可能具有可拉伸的重复部分,则可以拉伸图像的内部而无需创建新的图像。 UIImage中有两个API称为:
– resizableImageWithCapInsets:仅ios5)
– stretchableImageWithLeftCapWidth:topCapHeight:在iOS 5.0中已弃用)
如果找到正确的插图,则可以避免创建新图像,基本上是与在消息应用程序中创建气泡的系统相同。 气球确实很小,但它具有可拉伸的区域,可保持长宽比。
由于图像非常简单,因此您也可以考虑使用Quartz函数创建矩形路径来绘制图像,因此路径“几乎”与分辨率无关,只需要缩放它们的正确大小即可。

暂无
暂无

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

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