Overlay an Image Over Another Image in iOS

how to overlay an image over an image in xcode?

Create two UIImageViews for each image and add the foreground one as a subview to the parent.

Something like this:

let bgimg = UIImage(named: "image-name") // The image used as a background
let bgimgview = UIImageView(image: bgimg) // Create the view holding the image
bgimgview.frame = CGRect(x: 0, y: 0, width: 500, height: 500) // The size of the background image

let frontimg = UIImage(named: "front-image") // The image in the foreground
let frontimgview = UIImageView(image: frontimg) // Create the view holding the image
frontimgview.frame = CGRect(x: 150, y: 300, width: 50, height: 50) // The size and position of the front image

bgimgview.addSubview(frontimgview) // Add the front image on top of the background

Overlay an image over another image in iOS

If you are concerned with table scrolling performance, retrieve the thumbnail and paint the play button over it.

+(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake( point.x, point.y, fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

Saving an image on top of another image in Swift

I would recommend reading through this thread. All your answers are there. Once you read through that article, the following code sample should help you composite the two images together properly.

func saveImage() {
let bottomImage = UIImage(named: "bottom")!
let topImage = UIImage(named: "top")!

let newSize = CGSizeMake(100, 100) // set this to what you need
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

bottomImage.drawInRect(CGRect(origin: CGPointZero, size: newSize))
topImage.drawInRect(CGRect(origin: CGPointZero, size: newSize))

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

Hopefully this gets you going in the right direction.

iPhone, how does one overlay one image onto another to create a new image to save? (watermark)

It's pretty easy:

UIImage *backgroundImage = [UIImage imageNamed:@"image.png"];
UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"];

UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

If you want the background and watermark to be of the same size then use this code

...
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
...

How to overlay one image over another image in iPhone

You draw the 2 images in the wrong order. Transparency is a property that makes things shine through that have been painted previously. Therefore you have to draw the (opaque) photo first and the (transparent) overlay second.

Swift how to place pictures on top of pictures

Another more simple implementation may be to use a UIImageView as a parent view, then add a UIImageView as a subview for any images you wish to overlay on top of the original.

let mainImage = UIImage(named:"main-pic")
let overlayImage = UIImage(named:"overlay")

var mainImageView = UIImageView(image:mainImage)
var overlayImageView = UIImageView(image:overlayImage)

self.view.addSubview(mainImageView)
mainImageview.addSubview(overlayImageView)

Edit: Since this has become the accepted answer, I feel it is worth mentioning that there are also different options for positioning the overlayImageView: you can add the overlay to the same parent after the first view has been added, or you can add the overlay as a subview of the main imageView as the example demonstrates.

The difference is the frame of reference when setting the coordinates for your overlay frame: whether you want them to have the same coordinate space, or whether you want the overlay coordinates to be relative to the main image rather than the parent.



Related Topics



Leave a reply



Submit