簡體   English   中英

Objective-C-如何繪制四邊形圖像?

[英]Objective-c - How to draw an image as a quadrilateral?

這是我在Objective-C中的第一個月,我試圖在另一個圖像(背景圖像)上繪制一個四邊形的UIImage (ImageA

圖像A

在此處輸入圖片說明

背景圖 在此處輸入圖片說明

結束圖片 在此處輸入圖片說明

到目前為止,我一直在嘗試借助https://stackoverflow.com/a/18606029/864513對包含ImageA的UIImageView進行3D轉換。

然后拍攝轉換后的視圖的快照

- (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;
}

然后在“圖像上下文”中將快照圖像與背景圖像一起繪制為矩形

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

此方法效果很好,但是在循環中使用時可以創建GIF 它會在低內存設備中導致大量內存泄漏崩潰

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

在Android中,有一個名為drawBitmapMesh的函數可通過網格繪制位圖。 https://developer.android.com/reference/android/graphics/Canvas.html#drawBitmapMesh(android.graphics.Bitmap、int、int、float[]、int、int[]、int、android.graphics.Paint

有什么方法可以在Objective-C中繪制網格。 或者,如果您知道解決我的問題的更好方法,我將永遠感激不已。 謝謝

您可以在圖像上使用PerspectiveTransform過濾器。

示例代碼: Github

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM