@@ -95,15 +95,13 @@ - (id)initWithLanguage:(NSString *)language
9595 cachesRelatedDataPath : (NSString *)cachesRelatedPath
9696 engineMode : (G8OCREngineMode)engineMode
9797{
98- // config Tesseract to search trainedData in tessdata folder of the Caches folder
99- NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
100- NSString *cachesPath = cachesPaths.firstObject ;
98+ NSString *absoluteDataPath = nil ;
99+ if (cachesRelatedPath) {
100+ // config Tesseract to search trainedData in tessdata folder of the Caches folder
101+ NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
102+ NSString *cachesPath = cachesPaths.firstObject ;
101103
102- NSString *absoluteDataPath = [cachesPath stringByAppendingPathComponent: cachesRelatedPath].copy ;
103-
104- BOOL success = [self moveTessdataToCachesDirectoryIfNecessary ];
105- if (success == NO ) {
106- return nil ;
104+ absoluteDataPath = [cachesPath stringByAppendingPathComponent: cachesRelatedPath].copy ;
107105 }
108106 return [self initWithLanguage: language
109107 configDictionary: configDictionary
@@ -123,7 +121,12 @@ - (id)initWithLanguage:(NSString *)language
123121 if (configFileNames) {
124122 NSAssert ([configFileNames isKindOfClass: [NSArray class ]], @" Error! configFileNames should be of type NSArray" );
125123 }
126-
124+ if (absoluteDataPath) {
125+ BOOL moveDataSuccess = [self moveTessdataToDirectoryIfNecessary: absoluteDataPath];
126+ if (moveDataSuccess == NO ) {
127+ return nil ;
128+ }
129+ }
127130 _absoluteDataPath = [absoluteDataPath copy ];
128131 _language = [language copy ];
129132 _configDictionary = configDictionary;
@@ -218,14 +221,14 @@ - (BOOL)resetEngine
218221 return isInitDone;
219222}
220223
221- - (BOOL )moveTessdataToCachesDirectoryIfNecessary
224+ - (BOOL )moveTessdataToDirectoryIfNecessary : ( NSString *) directoryPath
222225{
223226 NSFileManager *fileManager = [NSFileManager defaultManager ];
224227
225228 // Useful paths
226229 NSString *tessdataFolderName = @" tessdata" ;
227230 NSString *tessdataPath = [[NSBundle mainBundle ].resourcePath stringByAppendingPathComponent: tessdataFolderName];
228- NSString *destinationPath = [self .absoluteDataPath stringByAppendingPathComponent: tessdataFolderName];
231+ NSString *destinationPath = [directoryPath stringByAppendingPathComponent: tessdataFolderName];
229232 NSLog (@" Tesseract destination path: %@ " , destinationPath);
230233
231234 if ([fileManager fileExistsAtPath: destinationPath] == NO ) {
0 commit comments