需要立即捕捉屏幕

时间:2012-05-03 13:18:59

标签: ios uipageviewcontroller

所以,我正在使用此功能来拍摄加载电子书的UIWebView并在用户渲染页面时显示它

-(UIImage*)captureScreen:(UIView*) viewToCapture
{

    UIGraphicsBeginImageContextWithOptions(viewToCapture.bounds.size, viewToCapture.opaque, 0.0);
    [viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

但问题是有一个延迟(0.5秒)恰好得到图像..当我在时间分析器中测试时,仪器指向此行[viewToCapture.layer renderInContext:UIGraphicsGetCurrentContext()];作为导致延迟的那一行..所以任何建议,建议我怎么能克服这个延迟。

先谢谢。

备注: 1.我使用UIPageController进行书籍效果。

2.检查我的尝试,请查看link

2 个答案:

答案 0 :(得分:1)

解决此问题的最佳方法是使用委托。这意味着,如果你告诉委托方法制作图像,它可以在后台执行并告诉你的视图“嘿,现在我为你找到了XXX的图像”(取决于你如何实现它)。 通过这种方式,您可以在其中加载包含电子书的视图,并在默认背景下显示书中间的加载程序。完成图像后,使用正确的图像更新图书的视图并删除加载器。 就像Apple的iBooks和其他任何好的应用程序一样。

来自我自己的一个项目的示例(根据您的需要,但在UITableViewController中使用):
BookDelegate.h

#import <Foundation/Foundation.h>

@class Book;

@protocol BookDelegate <NSObject>

@optional
- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath;

@end

<强> Book.h

#import <Foundation/Foundation.h>
#import "BookDelegate.h"

@interface Book : NSOperation <NSObject>
{   
    id <BookDelegate> delegate;
    SEL didRecieveImageForBookSelector;
}

@property (strong, nonatomic) id delegate;
@property (assign) SEL didRecieveImageForBookSelector;

- (NSString*)getBookImageForBookId:(int)BookId externalRefference:(NSString*)url indexPath:(NSIndexPath*)indexPath;
- (id)delegate;

// Delegate methods
- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath;

@end

<强> Book.m

#import "Book.h"
#import <objc/runtime.h>

@implementation Book

static char kAssociationKey;

@synthesize didRecieveImageForBookSelector;
@synthesize delegate;

- (id)init
{
    if (self = [super init])
    {
        [self setDidRecieveImageForBookSelector:@selector(didRecieveImageForBook:indexPath:)];
    }
    return self;
}

#pragma mark -
#pragma mark The default delegate functions

- (void)didRecieveImageForBook:(NSString*)imagePath indexPath:(NSIndexPath*)indexPath
{
    NSLog(@"********************************************************");
    NSLog(@"*** PLEASE IMPLEMENT THE FOLLOWING DELEGATE FUNCTION ***");
    NSLog(@"***       didRecieveImageForBook:indexPath:       ***");
    NSLog(@"********************************************************");
}

#pragma mark -
#pragma mark Function for fechting images

// This method is not adapted to what YOU need, but left my code here in case it might help you out.
- (NSString*)getBookImageForBookId:(int)bookId externalRefference:(NSString*)url indexPath:(NSIndexPath*)indexPath
{    
    NSString *ext = [[url lastPathComponent] pathExtension];

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *imagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d.%@", APP_BookIMAGEPEFIX, BookId, ext]];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];

    if (fileExists)
    {
        return imagePath;
    }
    else {
        NSURL *theUrl = [NSURL URLWithString:url];

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:theUrl];
        [request setDidFinishSelector:@selector(BookImageFetched:)];
        [request setDidFailSelector:@selector(processFailed:)];
        [request setTimeOutSeconds:60];
        [request setDownloadDestinationPath:imagePath];
        [request setDelegate:self];

        [request startAsynchronous];

        objc_setAssociatedObject(request, &kAssociationKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

        return @"";
    }
}

- (void)BookImageFetched:(ASIHTTPRequest *)request 
{
    NSIndexPath *indexPath = objc_getAssociatedObject(request, &kAssociationKey);
    NSString *imagePath = request.downloadDestinationPath;

    [[self delegate] performSelector:self.didRecieveImageForBookSelector withObject:imagePath withObject:indexPath];
}

#pragma mark -
#pragma mark delegate functions

- (id)delegate
{
        return delegate;
}

- (void)setDelegate:(id)newDelegate
{
        delegate = newDelegate;
}

#pragma mark -

@end

答案 1 :(得分:1)

您可以使用GCD - 我注意到您提供的链接中GCD缺少一个步骤。这是我用来异步获取图像并在准备就绪时通知它并且工作正常的方法:

dispatch_queue_t concurrent = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(concurrent, ^{
        __block UIImage *image = nil;
        dispatch_sync(concurrent, ^{
            //put code to grab image here
        });
        dispatch_sync(dispatch_get_main_queue(), ^{
            //this gets called when the above is finshed
            //you should also check if the image is nil or not
        });
    }); 

希望有所帮助

用于记录 - 我使用它来获取UIView快照,并且总是尝试将我的目标放在父视图中,即使它是暂时的 - 它似乎加快了它。

UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];

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

不确定它是否有帮助,或者您是否使用相同的方法。我希望你尽快解决这个问题:)