简体   繁体   中英

How to save a scaled uiimage

the screen image link , the savePic image link ,You can find the different between screenImg and savePic. I set the backgroundImageView a very small png. and i want save the backgroundimageview to a Picture。 In the iPhone Simulator Screen, the edge of image is normal, but the edge of savePic is not clear. Anybody can tell me how to save a high definition picture.

- (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");
}

Using a small image, is impossible to scale it the correct way to a bigger image, you will always go through blurry, noisy images BUT if you image could have stretchable repetitive parts, you can stretch the interior of the image without creating a new one. There are two API in UIImage called:
– resizableImageWithCapInsets: (Only ios5)
– stretchableImageWithLeftCapWidth:topCapHeight: (Deprecated in iOS 5.0)
If you find the correct inset you can avoid to create a new image, basically is the same system used to create the bubble in message application. The balloon is really small but it has a stretchable area that maintain the aspect ratio.
Since the image is really simple you could also think to draw it using Quartz functions creating a rect path, paths are "almost" res independent just need to scale them the correct size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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