CALayer比例不受框架影响?

发布于 2024-10-15 05:28:58 字数 834 浏览 1 评论 0原文

我正在添加一个包含 CGImage 内容的子图层。我创建了它的框架,我认为它包括它的边界和位置。我还设置了图层contentsGravity,因此图像可以在UIImageView中正确调整大小,但imageview只显示图像的一部分,表明CALayer没有正确缩放图像。这里究竟发生了什么?为什么 CALayer 没有正确的比例?

-(void)setImageInView:(UIImage*)img {
    CALayer *mosaicLayer = [CALayer layer];

    mosaicLayer.frame = CGRectMake(0.0f,0.0f, 720.0f, 960.0f);
    mosaicLayer.contentsGravity = kCAGravityResizeAspect;
    mosaicLayer.contents = (id)[img CGImage];

    [imageView.layer addSublayer:mosaicLayer];

    UIGraphicsBeginImageContext(mosaicLayer.bounds.size);
    [mosaicLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *saver = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   }

I'm adding a sublayer with contents of a CGImage. I create it's frame, which i thought included it's bounds and position. I also set the layers contentsGravity, so the image would resize properly in the UIImageView, but the imageview only shows part of the image, indicating that the CALayer didn't scale the image properly. What's really going on here? Why doesn't the CALayer have the right scale?

-(void)setImageInView:(UIImage*)img {
    CALayer *mosaicLayer = [CALayer layer];

    mosaicLayer.frame = CGRectMake(0.0f,0.0f, 720.0f, 960.0f);
    mosaicLayer.contentsGravity = kCAGravityResizeAspect;
    mosaicLayer.contents = (id)[img CGImage];

    [imageView.layer addSublayer:mosaicLayer];

    UIGraphicsBeginImageContext(mosaicLayer.bounds.size);
    [mosaicLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *saver = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(saver, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文