首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将UIGraphicsContext图像保存为.png而不是.jpg

将UIGraphicsContext图像保存为.png而不是.jpg
EN

Stack Overflow用户
提问于 2015-02-07 19:02:11
回答 1查看 1.2K关注 0票数 1

我有这样的代码,可以将我的图像调整为缩略图:

代码语言:javascript
复制
//MAKE THUMBNAIL

    CGSize origImageSize = chosenImage.size;

    //the rectangle of the thumbnail;
    CGRect newRect = CGRectMake(0, 0, 70, 70);

    //scaling ratio
    float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);


    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);


    CGRect projectRect;
    projectRect.size.width= ratio*origImageSize.width;
    projectRect.size.height=ratio*origImageSize.height;
    //center the image
    projectRect.origin.x= ((newRect.size.width-projectRect.size.width)/2);
    projectRect.origin.y=((newRect.size.height-projectRect.size.height)/2);
    [chosenImage drawInRect:projectRect];

    //get the image from the image context
    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
    self.thumbnailView.image=smallImage;

    UIImageWriteToSavedPhotosAlbum(smallImage, self, nil, nil); //20kb
    UIGraphicsEndImageContext();

它将图像保存为jpg,我是否可以将其保存为png呢?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-07 19:07:31

尝试将图像的数据转换为PNG数据表示,然后返回到PNG映像,例如:

代码语言:javascript
复制
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *pngImageData =  UIImagePNGRepresentation(smallImage);
UIImage *pngSmallImage = [UIImage imageWithData:pngImageData];
UIImageWriteToSavedPhotosAlbum(pngSmallImage, self, nil, nil); //20kb
UIGraphicsEndImageContext();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28386126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档