|
@@ -315,6 +315,7 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
NSData *imageData = [NSData dataWithContentsOfFile:path];
|
|
|
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
|
|
|
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
|
|
|
+ CFRelease(imageSource); // 添加这行
|
|
|
CGContextSaveGState(pdfContext);
|
|
|
CGContextDrawImage(pdfContext, pageRect, imageRef);
|
|
|
CGContextRestoreGState(pdfContext);
|
|
@@ -407,6 +408,7 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
NSData *imageData = image.TIFFRepresentation;
|
|
|
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
|
|
|
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
|
|
|
+ CFRelease(imageSource); // 添加这行
|
|
|
CGContextSaveGState(pdfContext);
|
|
|
CGContextDrawImage(pdfContext, pageRect, imageRef);
|
|
|
CGContextRestoreGState(pdfContext);
|
|
@@ -554,11 +556,16 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
|
|
|
- (void)recognitionAppleImage:(NSImage *)image {
|
|
|
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
- __block typeof(self) blockSelf = self;
|
|
|
- _appleRequest = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
|
|
|
+ if(self->_appleRequest) {
|
|
|
+ [self->_appleRequest cancel];
|
|
|
+ self->_appleRequest = nil;
|
|
|
+ }
|
|
|
+ __weak typeof(self) weakSelf = self;
|
|
|
+ self->_appleRequest = [[VNRecognizeTextRequest alloc] initWithCompletionHandler:^(VNRequest * _Nonnull request, NSError * _Nullable error) {
|
|
|
+ __strong typeof(weakSelf) strongSelf = weakSelf;
|
|
|
NSArray *results = nil;
|
|
|
if (request.results.count > 0) {
|
|
|
- results = [blockSelf responseDataRequest:request dictionary:nil imageSize:image.size];
|
|
|
+ results = [strongSelf responseDataRequest:request dictionary:nil imageSize:image.size];
|
|
|
}
|
|
|
|
|
|
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
|
|
@@ -574,21 +581,21 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
if (error || !results) {
|
|
|
- if (self.delegate && [blockSelf.delegate respondsToSelector:@selector(GOCRManager:didFailureOCRImageAtIndex:error:)]) {
|
|
|
- [blockSelf.delegate GOCRManager:blockSelf didFailureOCRImageAtIndex:blockSelf.finishIndex error:error];
|
|
|
+ if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(GOCRManager:didFailureOCRImageAtIndex:error:)]) {
|
|
|
+ [strongSelf.delegate GOCRManager:strongSelf didFailureOCRImageAtIndex:strongSelf.finishIndex error:error];
|
|
|
}
|
|
|
} else {
|
|
|
- if (self.delegate && [blockSelf.delegate respondsToSelector:@selector(GOCRManager:didFinishOCRImageAtIndex:results:)]) {
|
|
|
- [blockSelf.delegate GOCRManager:blockSelf didFinishOCRImageAtIndex:blockSelf.finishIndex results:results];
|
|
|
+ if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(GOCRManager:didFinishOCRImageAtIndex:results:)]) {
|
|
|
+ [strongSelf.delegate GOCRManager:strongSelf didFinishOCRImageAtIndex:strongSelf.finishIndex results:results];
|
|
|
}
|
|
|
}
|
|
|
- [blockSelf recognitionAppleImageAtIndex:blockSelf.finishIndex+1];
|
|
|
+ [strongSelf recognitionAppleImageAtIndex:strongSelf.finishIndex+1];
|
|
|
});
|
|
|
}];
|
|
|
self->_appleRequest.usesCPUOnly = YES;
|
|
|
self->_appleRequest.recognitionLevel = self.appleRecognitionMode;
|
|
|
if (self.languages.count > 0) {
|
|
|
- NSMutableArray *array = [[NSMutableArray alloc] initWithArray:blockSelf.languages];
|
|
|
+ NSMutableArray *array = [[NSMutableArray alloc] initWithArray:self.languages];
|
|
|
if ([self.languages containsObject:@"zh-Hant"]) {
|
|
|
[array removeObject:@"zh-Hant"];
|
|
|
[array insertObject:@"zh-Hant" atIndex:0];
|
|
@@ -603,10 +610,12 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
self->_appleRequest.recognitionLanguages = @[@"zh-Hans",@"zh-Hant"];
|
|
|
}
|
|
|
NSError *error = nil;
|
|
|
- VNImageRequestHandler *handle = [[VNImageRequestHandler alloc] initWithCGImage:[self nsImageToCGImageRef:image] options:@{}];
|
|
|
+ CGImageRef imageRef = [self nsImageToCGImageRef:image];
|
|
|
+ VNImageRequestHandler *handle = [[VNImageRequestHandler alloc] initWithCGImage:imageRef options:@{}];
|
|
|
if (self->_appleRequest){
|
|
|
[handle performRequests:@[self->_appleRequest] error:&error];
|
|
|
}
|
|
|
+ CGImageRelease(imageRef);
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -840,19 +849,19 @@ static inline NSFont * FontWithSize(NSString *strChar, CGSize size) {
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
-- (CGImageRef)nsImageToCGImageRef:(NSImage*)image;
|
|
|
-{
|
|
|
- NSData * imageData = [image TIFFRepresentation];
|
|
|
- CGImageRef imageRef;
|
|
|
- if(imageData)
|
|
|
- {
|
|
|
- CGImageSourceRef imageSource =
|
|
|
- CGImageSourceCreateWithData(
|
|
|
- (CFDataRef)imageData, NULL);
|
|
|
- imageRef = CGImageSourceCreateImageAtIndex(
|
|
|
- imageSource, 0, NULL);
|
|
|
- }
|
|
|
- return imageRef;
|
|
|
+- (CGImageRef)nsImageToCGImageRef:(NSImage*)image {
|
|
|
+ NSData *imageData = [image TIFFRepresentation];
|
|
|
+ CGImageRef imageRef = NULL;
|
|
|
+
|
|
|
+ if (imageData) {
|
|
|
+ CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
|
|
|
+ if (imageSource) {
|
|
|
+ imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
|
|
|
+ CFRelease(imageSource); // 释放 imageSource
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return imageRef; // 调用者负责释放
|
|
|
}
|
|
|
|
|
|
@end
|