2011-09-05 117 views
19

我在iOS應用中顯示來自YouTube的縮略圖圖像。點擊後,它將轉到youtube。將圖像覆蓋在iOS中的另一幅圖像上

我需要一種將播放按鈕疊加到這些圖像上的方法。什麼可能是最直接的方式呢?

此外,圖像遠程加載到一個表,所以性能是一個很大的考慮

+0

如果我們把我們的覆蓋圖像的縮略圖imageview的頂部?你試過了嗎?我正在嘗試同樣的事情並對您的解決方案感到好奇。 – user739711

回答

61

如果您關心表的滾動瀏覽效果,檢索縮略圖,並在其上繪製的播放按鈕。

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

謝謝@jano!會試試這個。我實際上很驚訝的是,在圖像上繪畫將是有效的 –

+2

@Jano這可以工作,但png中的透明度會丟失。 –

+0

注意創建上下文時使用的0.0。使用0.0會使尺寸取決於主屏幕的比例。 –

3

這是我做的。 cameraImg是我從相機獲得的圖像,其他三個圖像是我在cameraImg上顯示的靜態圖像。在這種情況下,大小定義了我們要開始的上下文的大小。圖像繪製在由DrawInRect方法定義的矩形中。確保結束上下文並完成。

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

雨燕2.2版本

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

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