Search code examples
iosobjective-cuiimagegpuimagecatransform3d

Use CATransform3D to rotate an UIImage and only get a cropped new UIImage


I'm trying to use CATransform3DRotate method to rotate an UIImage around its X axis, and get the rotated UIImage, but after rotation, some part will be cropped, and some part will be left black(transparent).

I'm using Brad Larson's GPUImage framework to do this.

My code is

filter = [[GPUImageTransformFilter alloc] init];
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = 0.4;
perspectiveTransform.m33 = 0.4;
perspectiveTransform = CATransform3DRotate(perspectiveTransform, 0.75, 1.0, 0.0, 0.0);


[(GPUImageTransformFilter *)filter setTransform3D:perspectiveTransform];
GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:_myImage smoothlyScaleOutput:YES];
[sourcePicture addTarget:filter];
[filter useNextFrameForImageCapture];
[sourcePicture processImage];

_myImage = [filter imageFromCurrentFramebuffer];

Is there a way I can let the new UIImage suit the new size with out scaling down? Because scaling down may reduce the quality of the picture.

BTW, I tried to do it on a CALayer, but although the CALayer looks rotated, the actual UIImage is still the same.

Thanks heaps in advance!


Solution

  • If you use UIImageView to display your image, then you can use the UIView's transform property to achieve what you need; please take a look at this answer for example. Alternatively, if you need UIImage rotated itself (not an image view), then please take a look at this answer.