Objective-c - 如何将图像绘制为四边形?

时间:2022-09-07 11:12:56

it's my first month in objective-c, I'm trying to draw an UIImage (ImageA) as a quadrilateral on top of another image (Background Image)

这是我在objective-c的第一个月,我试图在另一个图像上绘制一个UIImage(ImageA)作为四边形(背景图像)

ImageA

Objective-c  - 如何将图像绘制为四边形?

Background Image Objective-c  - 如何将图像绘制为四边形?

End Image Objective-c  - 如何将图像绘制为四边形?

What I've tried so far is to 3D Transform the UIImageView containing ImageA with the help of https://*.com/a/18606029/864513

到目前为止我尝试过的是在https://*.com/a/18606029/864513的帮助下对包含ImageA的UIImageView进行3D转换

Then take a snapshot of the transformed view

然后拍摄转换后的视图的快照

- (UIImage *)snapshot:(UIView *)view
                size:(CGSize)size
{
  UIImage *viewImage = nil;
  @autoreleasepool {
    UIGraphicsBeginImageContext(size);
    BOOL success = [view drawViewHierarchyInRect:(CGRect){CGPointZero, size} afterScreenUpdates:YES];
    viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (!success || !viewImage) {
      return nil;
    }
  }
  return viewImage;
}

Then draw the snapshot image in rectangle in Image Context along with Background Image

然后在“图像上下文”中绘制矩形快照图像以及背景图像

CGRect offsetRect = CGRectMake(0, 0, fileSize.width, fileSize.height);
UIGraphicsBeginImageContext(fileSize);
[BackgroundImage drawInRect:offsetRect];
[ImageA drawInRect:offsetRect];
UIImage *endImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This method works well but when used in a loop to create a GIF. It causes a lot of memory leaks and crashes in low memory devices!

此方法效果很好,但在循环中用于创建GIF时。它会在低内存设备中导致大量内存泄漏和崩溃!

CGImageDestinationAddImage(destination, [endImage CGImage], (__bridge CFDictionaryRef)frameProperties);

In Android there is a function called drawBitmapMesh that draw the bitmap through the mesh. https://developer.android.com/reference/android/graphics/Canvas.html#drawBitmapMesh(android.graphics.Bitmap, int, int, float[], int, int[], int, android.graphics.Paint)

在Android中有一个名为drawBitmapMesh的函数,它通过网格绘制位图。 https://developer.android.com/reference/android/graphics/Canvas.html#drawBitmapMesh(android.graphics.Bitmap,int,int,float [],int,int [],int,android.graphics.Paint)

Is there any way I can draw through the mesh in objective-c. Or if you know a better method to solve my problem, I will forever grateful. Thanks

有什么方法可以在objective-c中绘制网格。或者如果你知道一个更好的方法来解决我的问题,我将永远感激。谢谢

1 个解决方案

#1


1  

You can use PerspectiveTransform filter on your image.

您可以在图像上使用PerspectiveTransform过滤器。

Sample code: Github

示例代码:Github

#1


1  

You can use PerspectiveTransform filter on your image.

您可以在图像上使用PerspectiveTransform过滤器。

Sample code: Github

示例代码:Github