【问题标题】:UIImageView perspective tiltUIImageView 透视倾斜
【发布时间】:2019-10-17 13:40:06
【问题描述】:

我想要什么:https://imgur.com/zE6kgb1

我的成就:https://imgur.com/8Ylq5yp

以下是我正在使用的功能:

- (void) showViewWithImageFromAsset: (PHAsset *) asset
 {
     UIView *imageContainer = [[UIView alloc] initWithFrame:CGRectMake(25, 320, 400, 400)];
     [imageContainer setBackgroundColor:[UIColor whiteColor]];
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 250, 200)];


     CGFloat shadowRadius = 0.0;
     CGFloat width = imageView.frame.size.width;
     CGFloat height = imageView.frame.size.height; // Get width and height of the view

// Plot the path
UIBezierPath *shadowPath = [[UIBezierPath alloc] init];
[shadowPath moveToPoint:CGPointMake(width, 0)];
[shadowPath addLineToPoint:CGPointMake((width)+10, 10)];
[shadowPath addLineToPoint:CGPointMake((width)+10, height-10)];
[shadowPath addLineToPoint:CGPointMake(width, height)];

imageView.layer.shadowColor = UIColor.blackColor.CGColor;
imageView.layer.shadowPath = shadowPath.CGPath;
imageView.layer.shadowRadius = shadowRadius;
imageView.layer.shadowOffset = CGSizeZero;
imageView.layer.shadowOpacity = 1.0;

PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = YES;

PHImageManager *manager = [PHImageManager defaultManager];

// assets contains PHAsset objects.
__block UIImage *ima;

[manager requestImageForAsset:asset
                   targetSize:PHImageManagerMaximumSize
                  contentMode:PHImageContentModeDefault
                      options:requestOptions
                resultHandler:^void(UIImage *image, NSDictionary *info) {
                    ima = image;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [imageView setImage:ima];

                        CATransform3D t = CATransform3DIdentity;
                        t.m34 = .005;
                        imageContainer.layer.sublayerTransform = t;
                        imageView.layer.transform = CATransform3DMakeRotation(-10,0,1,0);
                        [imageContainer addSubview:imageView];
                        //[imageContainer addSubview:imageSideView];
                        [self.view addSubview:imageContainer];
                        [self.view bringSubviewToFront:imageView];
                    });

                }];

}

如您所见,尽管使用倒置(或镜像)图像,但我已经达到了预期的效果。我不打算使用任何第三方框架/库来实现这一点,因为我知道可以通过修改现有代码来实现。

虽然我可以将 CATransform3DMakeRotation(-10,0,1,0) 中的 y 值从 1 更改为 0 以获得正确的图像方向,但这也会将视角更改为 https://imgur.com/yKL4NQA

我需要一些东西来获得正确的图像而不改变视角。任何帮助是极大的赞赏。

【问题讨论】:

    标签: ios objective-c uiimageview


    【解决方案1】:

    最大的问题是CATransform3DMakeRotation 的第一个参数必须是弧度,而不是度数。

    imageView.layer.transform = CATransform3DMakeRotation(-10 * M_PI / 180.0, 0, 1, 0);
    

    那你需要更改shadowPath的x:

    [shadowPath moveToPoint:CGPointMake(0, 0)];
    [shadowPath addLineToPoint:CGPointMake(-10, 10)];
    [shadowPath addLineToPoint:CGPointMake(-10, height-10)];
    [shadowPath addLineToPoint:CGPointMake(0, height)];
    

    这给出了想要的结果。

    【讨论】:

    • 谢谢你,成功了。弧度部分是我的缺失点。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-01-27
    • 2015-07-13
    • 1970-01-01
    • 2019-12-15
    • 2018-11-11
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多