CALayer比例不受框架影响?
我正在添加一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论