Ossama Benallouch
Ossama Benallouch

Reputation: 97

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

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)

ImageA

enter image description here

Background Image enter image description here

End Image enter image description here

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

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!

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)

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

Upvotes: 0

Views: 230

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3446

You can use PerspectiveTransform filter on your image.

Sample code: Github

Upvotes: 1

Related Questions