UIImageView透视图倾斜

时间:2019-06-01 00:28:03

标签: ios objective-c uiimageview

我想要的东西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的视角更改为

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

1 个答案:

答案 0 :(得分: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)];

这给出了预期的结果。