2011-09-05 11 views
19

sto visualizzando le immagini di anteprima da YouTube nella mia app iOS. che su clic, andrà su youtube.Sovrapponi un'immagine a un'altra immagine in iOS

Ho bisogno di un modo per sovrapporre un pulsante di riproduzione a quelle immagini. Quale potrebbe essere il modo più semplice per farlo?

Inoltre, le immagini vengono caricate in remoto su un tavolo, quindi le prestazioni è una grande considerazione

+0

se mettiamo la nostra sovrapposizione di immagine sulla parte superiore della miniatura-ImageView? Hai provato? Sto provando la stessa cosa e sono curioso della tua soluzione .. – user739711

risposta

61

Se siete interessati con tavolo scorrimento prestazioni, recuperare la miniatura e dipingere il pulsante di riproduzione su di esso.

+(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

grazie @jano! proverei questo. In realtà sono piuttosto sorpreso del fatto che dipingere su un'immagine sia efficiente –

+2

@Jano Questo funziona, ma la trasparenza nel png è persa .. –

+0

Prestare attenzione allo 0.0 utilizzato quando viene creato il contesto. L'uso di 0.0 rende le dimensioni dipendenti dalla scala della schermata principale. Il punto –

3

Ecco cosa ho fatto. La cameraImmagine è l'immagine che sto ricevendo dalla fotocamera e le altre tre immagini sono immagini statiche che ho visualizzato sulla fotocameraImg. In questo caso, la dimensione definisce, la dimensione per il contesto che stiamo per iniziare. Le immagini sono disegnate nel retto definito dal metodo DrawInRect. Assicurati di terminare il contesto e il gioco è fatto.

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 Versione

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! 
} 
Problemi correlati