菜鸟教程小白 发表于 2022-12-11 18:46:44

ios - ImageContext 返回扭曲的图像


                                            <p><p>我正在尝试使图像的角变圆,但 ImageContext 中的图像变形了。 </p>

<p>这是我正在采取的步骤:</p>

<pre><code>- (UIImage *)decodeBase64ToImage:(NSString *)strEncodedData
{
    NSURL *url = ;
    NSData* data = [ initWithContentsOfURL:url];
    UIImage* image = [ initWithData:data];
    UIImage* croppedImage = ;
    return croppedImage;
}

- (UIImage *)makeRoundedImage:(UIImage *) image
{

    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);

    UIGraphicsBeginImageContext(frame.size);
    //[ addClip];
    ;

    // Get the image
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();

    return croppedImage;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>你可以检查这个方法</strong> </p>

<pre><code>-(UIImage *)roundedImage:(UIImage *) roundImage
                      radius: (float) radius;
{
CALayer *imageLayer = ;
imageLayer.frame = CGRectMake(0, 0, roundImage.size.width, roundImage.size.height);
imageLayer.contents = (id) roundImage.CGImage;

imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;

UIGraphicsBeginImageContext(roundImage.size);
;
UIImage *finalRoundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return finalRoundedImage;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - ImageContext 返回扭曲的图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46423791/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46423791/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - ImageContext 返回扭曲的图像