2011-10-27 22 views
7

Voglio catturare una parte particolare dello schermo di iPhone. Ho usato UIGraphicsBeginImageContextWithOptions, ma non sono riuscito a catturare una porzione di schermo con questo. Per favore aiutatemi.ios come catturare una particolare porzione di schermo

+0

vuoi catturare una schermata dalla tua macchina o vuoi catturare un'area nella tua app tramite codice? – mAc

+2

@ mAc ovviamente vuole farlo con il codice..non è il motivo per cui usa UIGraphicsBeginImageContextWithOptions – Krishnabhadra

risposta

15

È possibile scattare una schermata utilizzando UIGraphicsGetImageFromCurrentContext. Scritto sotto il codice dalla memoria, quindi gli errori possibili. Si prega di correggerlo da soli.

- (UIImage*)captureView:(UIView *)yourView { 
    CGRect rect = [[UIScreen mainScreen] bounds]; 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [yourView.layer renderInContext:context]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
25

è possibile utilizzare questo codice

UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGRect rect = CGRectMake(250,61 ,410, 255); 
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef); 
+3

questo è utile quando devi catturare un'area specifica del tuo schermo. – priyanka

+2

fantastico compagno :) –

+0

Ho ottenuto la parte particolare da questo. Ma in alcuni punti di vista, sta nascondendo il suo contenuto. Puoi dare qualche idea del perché sta accadendo? – Niharika

0

Un altro metodo per catturare screenshot chiaro;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
      UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, NO, [UIScreen mainScreen].scale); 
     else 
      UIGraphicsBeginImageContext(self.captureView.frame.size); 

     [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
     [viewImage drawInRect:CGRectMake(0.0, 0.0, 640,960)]; 
     //NSData*m_Imgdata=UIImagePNGRepresentation(viewImage); 
     NSData*m_Imgdata=UIImageJPEGRepresentation(viewImage, 1.0); 

     // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

     UIGraphicsEndImageContext(); 
1

la risposta @Superdev è proprio sul posto, quello che voglio aggiungere è un piccolo trucco, se si vuole acquisire una vista genitore ed evitare subviews (in particolare vista overlay) che cosa si può fare è fare questo visualizzazione secondaria una proprietà e utilizzare il seguente

CGFloat width = CGRectGetWidth(self.view.bounds); 
CGFloat height = CGRectGetHeight(self.view.bounds); 
_overlayView.hidden = YES; 
UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGRect rect = CGRectMake(0,0 ,width, height); 
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef); 

_overlayView.hidden = NO; 

sua un piccolo trucco, spero che qualcuno lo troverà utile

3

Ecco un'estensione rapida sulla UIView che o catturare l'intera vista o una cornice all'interno della vista. Prende in considerazione la scala dello schermo.

extension UIView { 

    func imageSnapshot() -> UIImage { 
     return self.imageSnapshotCroppedToFrame(nil) 
    } 

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { 
     let scaleFactor = UIScreen.mainScreen().scale 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) 
     self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true) 
     var image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     if let frame = frame { 
      // UIImages are measured in points, but CGImages are measured in pixels 
      let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor)) 

      if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) { 
       image = UIImage(CGImage: imageRef) 
      } 
     } 
     return image 
    } 
} 
+1

Ciao, jDutton. Ho amato la tua risposta. Ho deciso di aggiornare il codice per Swift 3.0. –

+0

Grazie mille !! – Jerome

0

SWIFT 3.0 Xcode8 Versione provengono da @jDutton risposta a lavorare per me.

extension UIView { 
    func imageSnapshot() -> UIImage { 
     return self.imageSnapshotCroppedToFrame(frame: nil) 
    } 

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { 
     let scaleFactor = UIScreen.main.scale 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) 
     self.drawHierarchy(in: bounds, afterScreenUpdates: true) 
     var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     if let frame = frame { 
      // UIImages are measured in points, but CGImages are measured in pixels 
      let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)) 

      if let imageRef = image.cgImage!.cropping(to: scaledRect) { 
       image = UIImage(cgImage: imageRef) 
      } 
     } 
     return image 
    } 
} 

se si crash e il seguente messaggio:

GContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 

provare questo solution

speranza per salvare il vostro tempo!

Problemi correlati