簡體   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