gpt4 book ai didi

ios - 需要立即捕获屏幕

转载 作者:行者123 更新时间:2023-11-29 11:13:37 25 4
gpt4 key购买 nike

伙计们,我正在使用这个函数拍摄 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

最佳答案

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

来 self 自己项目之一的示例(根据您的需要进行调整,但用于 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

关于ios - 需要立即捕获屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com