2012-08-22 52 views
0

功能我有一個需要編寫一個應用程序,可以拍攝照片,然後我希望能夠預覽並覆蓋圖形,也許一些文字,然後將其保存回圖像庫。哪個圖形庫和疊加文本和圖形圖像上

所以,我的用戶將開始我的應用程序,然後它將使用的UIImagePickerController通過代表團訪問攝像頭並獲取圖像數據。

然後,我想在屏幕上顯示它,然後顯示控件以添加可見圖像(或特定標誌),併爲用戶提供添加一行或兩行文本的選項,然後將它們放置在圖片。

然後,用戶將點擊保存,最終的合成圖像將被寫入相機膠捲/照片庫,然後用戶可以根據需要將圖像上傳到Twitter,Facebook等(我不希望實施的開銷OAuth2可能是別人的問題)。

那麼,我需要知道的是,SDK中可用的許多庫中的哪一個會讓我這樣做? CGImage?並且,由於我的覆蓋圖標將是透明的PNG,我如何覆蓋另一個?

我聽說,你可以提出與CALayer的,但我不知道你又如何抓住所有的層結合在一起(拉平他們)。

在PHP中,你會使用類似imagecopymerge()的東西,我很難找出等價物。

我不想把數據傳送到網絡服務器和回來,因爲我知道它可以本地完成。

感謝

回答

2

那麼添加文字到圖像的我敢肯定,你可以只使用一個文本框來獲取輸入然後把文本標籤。這部分可以通過幾種不同的方式完成,但相當容易。

至於抓住所有層的組合圖像,你可以使用Quartz。

UIWindow(從UIView繼承)和UIView支持CALayer。 CALayer/-renderInContext:方法可讓您將圖層及其子圖層渲染​​爲圖形上下文。因此,要獲取整個屏幕的快照,可以遍歷屏幕上的每個窗口並將其圖層層次結構呈現爲目標上下文。完成後,您可以通過UIGraphicsGetImageFromCurrentImageContext函數獲取屏幕截圖,如下所示。

需要注意的是,CALayer/-renderInContext:僅捕獲您的UIKit和Quartz繪製。它不捕獲OpenGL ES或視頻內容。

#import <QuartzCore/QuartzCore.h> 

.....

- (UIImage*)screenshot 
{ 
    // Create a graphics context with the target size 
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration 
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext 
    CGSize imageSize = [[UIScreen mainScreen] bounds].size; 
    if (NULL != UIGraphicsBeginImageContextWithOptions) 
     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 
    else 
     UIGraphicsBeginImageContext(imageSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Iterate over every window from back to front 
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    { 
     if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) 
     { 
      // -renderInContext: renders in the coordinate space of the layer, 
      // so we must first apply the layer's geometry to the graphics context 
      CGContextSaveGState(context); 
      // Center the context around the window's anchor point 
      CGContextTranslateCTM(context, [window center].x, [window center].y); 
      // Apply the window's transform about the anchor point 
      CGContextConcatCTM(context, [window transform]); 
      // Offset by the portion of the bounds left of and above the anchor point 
      CGContextTranslateCTM(context, 
            -[window bounds].size.width * [[window layer] anchorPoint].x, 
            -[window bounds].size.height * [[window layer] anchorPoint].y); 

      // Render the layer hierarchy to the current context 
      [[window layer] renderInContext:context]; 

      // Restore the context 
      CGContextRestoreGState(context); 
     } 
    } 

    // Retrieve the screenshot image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return image; 
} 

至於上傳到Facebook和Twitter看看ShareKit。這是一個拖放庫,與Facebook,Twitter,Tumblr,電子郵件等共享。這將增加您的應用程序的價值。我建議實施它。

+0

我喜歡這個主意,這哪是適應特定的UIView的工作,所以如果我在iPad上編寫應用程序,我可以顯示在縮放的UIView右側的組合圖像的預覽和左邊的選項,但只捕獲所有的UIView的?我想這將是迭代子視圖的情況? – JamesB

+0

正確的,而不是遍歷屏幕的所有窗口只是通過UIView的移動。 – random

+0

感謝cory,我剛剛發現你的鏈接到ShareKit以及我肯定會研究,目前,這是一個私人使用的應用程序(是不是他們如何開始),但可能會在我發佈後不久已經對它進行了試用。一個UIView將包含一個UIImage和一個UIImage可以設置爲可以擴展到包含視圖的權利?我試圖想到人們可以將徽標添加到設備上的方式,但只需將其添加到照片庫並從那裏選擇,並提供位置控制似乎是最友好的UI方式。 – JamesB