SORU
6 ŞUBAT 2010, CUMARTESİ


Nasıl bir UİView bir Ekran görüntüsü alabilirim?

İPhone uygulamam bir Uİİmage olarak belirli bir UİView ekran görüntüsünü almak nasıl merak ediyorum.

Ama Ben olsun tüm boş bir görüntüdür bu kodu denedim.

UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer drawInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

myUIView alt manzarasına sahip bir boyutları 320x480 vardır. Bunu yapmak için doğru yolu nedir?

CEVAP
20 EYLÜL 2013, Cuma


iOS 7 görünümü geçerli grafik içerik hiyerarşisi çekmek için izin veren yeni bir yöntem var. Bu bir Uİİmage çok hızlı almak için kullanılabilir.

UİView kategori metod Uİİmage olarak görebilmek için uygulanmaktadır:

- (UIImage *)pb_takeSnapshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);

    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];

    // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Mevcut renderİnContext çok daha hızlı o zaman: yöntem.

Referans : https://developer.apple.com/library/ios/qa/qa1817/_index.html

SWİFT İÇİN GÜNCELLEME: Aynı işi yapar bir uzantısı:

extension UIView {

    func pb_takeSnapshot() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)

        drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)

        // old style: layer.renderInContext(UIGraphicsGetCurrentContext())

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • disneychannel

    disneychanne

    19 ŞUBAT 2006
  • Friday NightFort

    Friday Night

    15 EYLÜL 2011
  • HDstarcraft

    HDstarcraft

    12 Mayıs 2009