Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a transform to a UIImage when drawing

Trying to superimpose a smaller image which may be scaled or rotated onto a lager image:

+ (UIImage*)addToImage:(UIImage *)baseImage newImage:(UIImage*)newImage atPoint:(CGPoint)point transform:(CGAffineTransform)transform {

    UIGraphicsBeginImageContext(baseImage.size);
    [baseImage drawInRect:CGRectMake(0, 0, baseImage.size.width, baseImage.size.height)];

    [newImage drawAtPoint:point];
    //How would I apply the transform?

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

return result;
}

How would I apply the CGAffineTransform to newImage in this context?

like image 556
soleil Avatar asked Jan 30 '14 22:01

soleil


People also ask

How do I add an image to Uiimage?

With Interface Builder it's pretty easy to add and configure a UIImageView. The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project).

What is UIImage in swift?

An object that manages image data in your app.


1 Answers

You can use the CoreImage framework, this allows transformations on CIImage instances. For example:

CGAffineTransform transform = ...;
CIImage* coreImage = newImage.CIImage;

if (!coreImage) {
    coreImage = [CIImage imageWithCGImage:newImage.CGImage];
}

coreImage = [coreImage imageByApplyingTransform:transform];
newImage = [UIImage imageWithCIImage:coreImage];

You'll need to ensure that after calling .CIImage, that is isn't nil. This will occur if the UIImage was initialised with a CGImage. If this is the case, then you'll need to allocate a CIImage yourself with the appropriate initialiser.

like image 105
WDUK Avatar answered Oct 23 '22 05:10

WDUK