SORU
1 Kasım 2011, Salı


'nın en hızlı, en sade programlama yoluyla yapmak şekilde ölçülebilir?iOS:

benim iPad app, bir UİView ekranın büyük bir bölümünü alarak bir ekran görüntüsü yapmak istiyorum. Ne yazık ki, subviews uzun ekran görüntüsü alın ve bir sayfa sonrasında kıvırmak animasyon o kadar çok iç içe.

Orada daha hızlı bir şekilde "her zamanki"?

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

Mümkünse, ya da benim görüşüme önbelleği yeniden yapılanma kaçmak istiyorum.

Yardımlarınız için çok fazla!

CEVAP
5 Kasım 2011, CUMARTESİ


Güncelleme

Mümkün olduğunda yeni iOS 7 anlık API kullanan daha iyi bir yöntem buldum.

Sihir burada

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

Yardımcı olur umarım.

  (UIImage *)screenshot
{
    CGSize imageSize = CGSizeZero;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }

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

Wanna know more about iOS 7 Snapshots?


Eski cevap

Technical Q&A iOS Geliştirici Kütüphane soruya cevap buldum, hadi bakalım:

(UİView alınan) her UİWindow ve UİView bir tarafından desteklenmektedir CALayer. /-RenderİnContext bu CALayer: yöntem bir katman oluşturma sağlar ve grafik bağlamında alt katmanlar. Yani, bir anlık kapmak için tüm ekran, ekranda her pencere içinde yineleme yapabilirsiniz hedef bağlamına katman hiyerarşisi oluşturma. Sen bitmiş bir kez ekran görüntüsü üzerinden alabilirsiniz UİGraphicsGetİmageFromCurrentİmagecontext aşağıda gösterildiği gibi işlev.

Core Animation API kullanmak için QuartzCore çerçeve eklemek gerekir böyle büyük mükafat projede gösterildiği gibi QuartzCore başlık aşağıdaki liste:


Liste 1: QuartzCore Başlığı Vardır

#import <QuartzCore/QuartzCore.h>

Liste 2: bir ekran görüntüsü Alın

- (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;
}

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • HowcastTechGadgets

    HowcastTechG

    22 EYLÜL 2010
  • ipsy

    ipsy

    1 EKİM 2012
  • MagicofRahat

    MagicofRahat

    13 Temmuz 2007