【问题标题】:How to save a scaled uiimage如何保存缩放的uiimage
【发布时间】:2012-06-19 05:56:58
【问题描述】:

the screen image link,the savePic image link,你可以找到screenImg和savePic的区别。 我将 backgroundImageView 设置为一个非常小的 png。我想将背景图像视图保存为图片。 在 iPhone 模拟器屏幕中,图像边缘正常,但 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");
}

【问题讨论】:

    标签: iphone uiimageview scale


    【解决方案1】:

    使用小图像,不可能以正确的方式将其缩放为更大的图像,您总是会看到模糊、嘈杂的图像但是如果您的图像可以具有可拉伸的重复部分,您可以在不创建图像的情况下拉伸图像的内部一个新的。 UIImage 中有两个 API 调用:
    – resizableImageWithCapInsets:(仅限 ios5)
    – stretchableImageWithLeftCapWidth:topCapHeight:(iOS 5.0 中已弃用)
    如果您找到正确的插图,您可以避免创建新图像,基本上是用于在消息应用程序中创建气泡的相同系统。气球真的很小,但它有一个可拉伸的区域,可以保持纵横比。
    由于图像非常简单,您也可以考虑使用 Quartz 函数创建矩形路径来绘制它,路径“几乎”与 res 无关,只需将它们缩放到正确的大小。

    【讨论】:

    • 感谢回复,问题不是小图如何显示,而是如何清晰保存图片。我使用小图像,因为只有当我使用小图像时,我才能找到错误
    猜你喜欢
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多