2011-09-05 27 views
19

Estoy mostrando imágenes en miniatura de youtube en mi aplicación iOS. que al hacer clic, irá a youtube.Superposición de una imagen sobre otra imagen en iOS

Necesito una forma de superponer un botón de reproducción a esas imágenes. ¿Cuál podría ser la forma más directa de hacerlo?

Además, las imágenes se cargan en forma remota una mesa, por lo que el rendimiento es una consideración importante

+0

si ponemos nuestra superposición de imagen en la parte superior de la miniatura-imageview? ¿Lo intentaste? Estoy intentando lo mismo y tengo curiosidad por tu solución. – user739711

Respuesta

61

Si usted está preocupado con el rendimiento del desplazamiento de mesa, recuperar la miniatura y pintar el botón de reproducción sobre ella.

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

gracias @jano! probaría esto. de hecho, estoy bastante sorprendido de que pintar sobre una imagen sea eficiente –

+2

@Jano Esto funciona pero la transparencia en el png se pierde. –

+0

Preste atención al 0.0 usado cuando se crea el contexto. Usar 0.0 hace que el tamaño dependa de la escala de la pantalla principal. –

3

Esto es lo que hice. La cámara es la imagen que obtengo de la cámara y las otras tres imágenes son imágenes estáticas que he mostrado en la cámara. En este caso, el tamaño define el tamaño del contexto que vamos a comenzar. Las imágenes se dibujan en el rect definido por el método DrawInRect. Asegúrate de finalizar el contexto y listo.

UIImage *cameraImg = image; 

UIImage *leftImg = [UIImage imageNamed:@"apple.jpeg"]; 

UIImage *rightImg = [UIImage imageNamed:@"Cloud.png"]; 

UIImage *middleImg = [UIImage imageNamed:@"mario.jpeg"]; 

CGSize size = CGSizeMake(cameraImg.size.width, cameraImg.size.height); 

UIGraphicsBeginImageContext(size); 

[cameraImg drawInRect:CGRectMake(0, 0, self.view.window.frame.size.width, self.view.window.frame.size.height)]; 

[leftImg drawInRect:CGRectMake(x, y, width, height)]; 

[rightImg drawInRect:CGRectMake(x, y, width, height)]; 

[middleImg drawInRect:CGRectMake(x, y, width, height)]; 

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,finalImage.size.width, finalImage.size.height)]; 

imageView.image = finalImage; 

[self.view addSubview:imageView]; 
5

Swift 2.2 Versión

static func drawImage(image foreGroundImage:UIImage, inImage backgroundImage:UIImage, atPoint point:CGPoint) -> UIImage{ 
    UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) 
    backgroundImage.drawInRect(CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)) 
    foreGroundImage .drawInRect(CGRectMake(point.x, point.y, foreGroundImage.size.width, foreGroundImage.size.height)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return newImage 
    } 
2

3.x Swift

func drawImage(image foreGroundImage:UIImage, inImage backgroundImage:UIImage, atPoint point:CGPoint) -> UIImage { 
    UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) 
    backgroundImage.draw(in: CGRect.init(x: 0, y: 0, width: backgroundImage.size.width, height: backgroundImage.size.height)) 
    foreGroundImage.draw(in: CGRect.init(x: point.x, y: point.y, width: foreGroundImage.size.width, height: foreGroundImage.size.height)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return newImage! 
} 
Cuestiones relacionadas