2010-12-17 6 views
7

Muy similar a this question (y también this answer), estoy tratando de hacer un UIImage desde una vista web. Hasta ahora estoy usando el código sugerido en la respuesta, en concreto:Convierte contenido de UIWebview en un UIImage cuando la vista web es más grande que la pantalla

UIGraphicsBeginImageContext(webview.bounds.size); 
[webview.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Sin embargo, cuando el contenido de la vista web son más grandes que la pantalla, las imágenes resultantes no son completos y tienen parches faltantes. ¿Alguna idea sobre cómo solucionar este problema?

Respuesta

17

Tengo la respuesta:

Hay que establecer el marco de la vista Web al contenido tamaño completo justo antes de crear la imagen. Después de esto, simplemente ajuste el tamaño a su origen:

+ (UIImage *) imageFromWebView:(UIWebView *)view 
{ 
    // tempframe to reset view size after image was created 
    CGRect tmpFrame   = view.frame; 

    // set new Frame 
    CGRect aFrame    = view.frame; 
    aFrame.size.height = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height; 
    view.frame    = aFrame; 

    // do image magic 
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]); 

    CGContextRef resizedContext = UIGraphicsGetCurrentContext(); 
    [view.layer renderInContext:resizedContext]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    // reset Frame of view to origin 
    view.frame = tmpFrame; 
    return image; 
} 
+0

Me gusta tanto cuando las personas publican la respuesta después de que la obtuvieron. esa es la manera de fluir! :) –

+0

Por cierto, me pregunto, ¿por qué es un singleton y no solo una función? Pregunto por curiosidad como alguien que todavía no usa singletons –

+0

Comienzo a entender por qué no puede funcionar con '-', pero ¿cómo uso el '+'? –

0

creo que esto podría ayudar a

UIGraphicsBeginImageContext([webView sizeThatFits:[[UIScreen mainScreen] bounds].size]]); 
3

La respuesta de D R está bien. Lo único que falta es tener en cuenta la escala de la pantalla. Lo he corregido en mi Swift y funciona bien para mí.

extension UIWebView{ 
func snapshotForCompletePage() -> UIImage { 
    // tempframe to reset view size after image was created 
    let tmpFrame: CGRect = self.frame 
    // set full size Frame 
    var fullSizeFrame: CGRect = self.frame 
    fullSizeFrame.size.height = self.scrollView.contentSize.height 
    self.frame = fullSizeFrame 

    // here the image magic begins 
    UIGraphicsBeginImageContextWithOptions(fullSizeFrame.size, false, UIScreen.mainScreen().scale) 
    let resizedContext: CGContextRef = UIGraphicsGetCurrentContext()! 
    self.layer.renderInContext(resizedContext) 
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    // reset Frame of view to origin 
    self.frame = tmpFrame 
    return image 
     } 
    } 
0

@ La respuesta de Jibeex funcionó para mí. Pero he tenido que añadir el siguiente código

fullSizeFrame.size.width = self.scrollView.contentSize.width 

continuación

fullSizeFrame.size.height = self.scrollView.contentSize.height 

para que funcione plenamente. Escribir como respuesta porque no tengo suficiente reputación para comentar.

Cuestiones relacionadas