2017-09-02 32 views
0

我该做什么:我对Objective-C非常陌生,在我旧的Macbook 2,1上适用于图片的线上应用程序。在Mac OS X 10.6.8和Xcode 3.2.6中的Objective-C中的命令行应用程序中打开离线jpg图像

我的问题:我只想获得保存在本地硬盘驱动器“Macintosh HD”上的图片的宽度和高度(int,NSInteger,NSNumber,...)。

对他人的相关性:必须有一个编程方式,以处理图片。这是非常基本的。

我的工作平台:我正在使用Xcode 3.2.6在Mac OS X 10.6.8上开发。

我的研究历史:我已经在stackoverflow和其他一些网站上寻找烹饪书籍和背景知识来做以下试验,但我还没有成功。我介绍了我在下面尝试的所有内容。事先没有任何工作。

//BildOeffner.h:

#import <Cocoa/Cocoa.h> 
@interface BildOeffner : NSView 
    -(BildOeffner*) init; 
    - (NSNumber*) extrahiereBildBreite:(NSString*) bildPfad; 
@end 

//BildOeffner.m:

#import "BildOeffner.h" 
@implementation BildOeffner 
    // extracts the image width 
    - (NSNumber*) extrahiereBildBreite:(NSString*) bildPfad{ 
    NSBitmapImageRep* nsImageRep = [NSBitmapImageRep imageRepWithContentsOfFile:bildPfad]; 
    NSInteger extrahierteBildBreite =[nsImageRep pixelsWide]; 
    NSNumber* bildBreite = [NSNumber numberWithInt:extrahierteBildBreite]; 
    return bildBreite; 
    } 
@end 

的代码显示在编辑器中没有编译错误,但也有在集结结果错误:误差消息:

Ld build/Debug/FotobuchErsteller_0 normal x86_64 
cd /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 
setenv MACOSX_DEPLOYMENT_TARGET 10.6 
/Developer/usr/bin/gcc-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk  -L/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug  -F/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug -filelist /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/Objects-normal/x86_64/FotobuchErsteller_0.LinkFileList -mmacosx-version-min=10.6 -framework Foundation -o /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug/FotobuchErsteller_0 

Undefined symbols: 
    "_OBJC_CLASS_$_NSImage", referenced from: 
    objc-class-ref-to-NSImage in BildOeffner.o 
    "_OBJC_METACLASS_$_NSView", referenced from: 
    _OBJC_METACLASS_$_BildOeffner in BildOeffner.o 
    "_OBJC_CLASS_$_NSView", referenced from: 
    _OBJC_CLASS_$_BildOeffner in BildOeffner.o 
    "_OBJC_CLASS_$_NSBitmapImageRep", referenced from: 
    objc-class-ref-to-NSBitmapImageRep in BildOeffner.o 
ld: symbol(s) not found 
collect2: ld returned 1 exit status 

然后我尝试以下:

//BildAnalyst.h:

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 
@interface BildAnalyst : NSObject 
@property (assign) NSArray* bilderListe; 
@property (assign) NSArray* bildObjektListe; 
@property (assign) NSNumber* erwarteteBildHoehe; 
@property (assign) NSNumber* erwarteteBildBreite; 

-(BildAnalyst*) init; 
-(BildAnalyst*) initWith:(NSArray*) neueBilderListe; 

- (NSArray *)bilderListe; 
- (void)setBilderListe:(NSArray *) neueBilderListe; 
- (NSArray *)bildObjektListe; 
- (void)setBildObjektListe:(NSArray *) neueBildObjektListe; 
- (NSNumber*)erwarteteBildHoehe; 
- (void)setErwarteteBildHoehe:(NSNumber*) neueErwarteteBildHoehe; 
- (NSNumber*)erwarteteBildBreite; 
- (void)setErwarteteBildBreite:(NSNumber*) neueErwarteteBildBreite; 

- (void) erstelleListeAllerBilderImOrdner:(NSString*) absoluterPfadZumOrdner; 
- (void) analysiereBilderListe; 
@end 

//BildAnalyst.m:

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 
#import "BildAnalyst.h" 
#import "Bild.h" 
@implementation BildAnalyst 

- (id)init { 
self = [super init]; 
    if (self){ 
    self.bilderListe = nil; 
    self.erwarteteBildHoehe = [NSNumber numberWithInt:3024]; 
    self.erwarteteBildBreite = [NSNumber numberWithInt:4032]; 
    } 
return self; 
} 

/*Der explizite Konstruktor dieser Klasse. 
Parameter: NSMutableArray neueBilderListe 
Rueckgabe: BildAnalyst bildAnalyst*/ 
-(BildAnalyst*) initWith:(NSMutableArray*) neueBilderListe{ 
self = [super init]; 
    if (self){ 
     self.bilderListe = neueBilderListe; 
    } 
return self; 
} 

- (void) setBilderListe:(NSArray *) neueBilderListe{ 
    self.bilderListe = neueBilderListe; 
} 

- (NSArray *) bilderListe{ 
    return self.bilderListe; 
} 

- (void)setBildObjektListe:(NSArray *) neueBildObjektListe{ 
    self.bildObjektListe = neueBildObjektListe; 
} 

- (NSArray *)bildObjektListe{ 
    return self.bildObjektListe; 
} 

- (NSNumber*)erwarteteBildHoehe{ 
    return self.erwarteteBildHoehe; 
} 

- (void)setErwarteteBildHoehe:(NSNumber*) neueErwarteteBildHoehe{ 
    self.erwarteteBildHoehe = neueErwarteteBildHoehe; 
} 

- (NSNumber*)erwarteteBildBreite{ 
    return self.erwarteteBildBreite; 
} 

- (void)setErwarteteBildBreite:(NSNumber*) neueErwarteteBildBreite{ 
    self.erwarteteBildBreite = neueErwarteteBildBreite; 
} 


/* Die folgende Methode dient dem Auslesen eines Ordners und dem Speichern von darin enthaltenen Objekten in erster Hierarchieebene in ein Array, welches die Instanzvariable "bilderListe" befuellt. 
Parameter NSString* absoluterPfadZumOrdner*/ 
- (void) erstelleListeAllerBilderImOrdner:(NSString*) absoluterPfadZumOrdner{ 
    NSError* fehlerBeimLesenDesBilderordners; 
    NSFileManager* nsFileManager = [NSFileManager defaultManager]; 
    [nsFileManager changeCurrentDirectoryPath:absoluterPfadZumOrdner]; 
    NSArray* dateiListe = [nsFileManager contentsOfDirectoryAtPath:absoluterPfadZumOrdner error:&fehlerBeimLesenDesBilderordners]; 
    [self setBilderListe:dateiListe]; 
} 

/*Die folgende Methode dient der Analyse der Bilder, welche in einem Ordner gefunden wurden. Sie bestimmt das Bildformat und speichert die Bilder als Objekte in die Instanzvariable "bildObjektListe". 
Parameter NSArray bilderListe*/ 
- (void) analysiereBilderListe { 
    NSUInteger i, count = [[self bilderListe] count]; 
    NSMutableArray* neueBildObjektListe = [[NSMutableArray alloc] init]; 
    for (i = 0; i < count; i++) { 
     NSString* bildPfad = [[self bilderListe] objectAtIndex:i]; 
     NSBitmapImageRep* nsImageRep = [NSBitmapImageRep imageRepWithContentsOfFile:bildPfad]; 
     NSInteger extrahierteBildHoehe = [nsImageRep pixelsHigh]; 
     NSNumber* bildHoehe = [NSNumber numberWithInt:extrahierteBildHoehe]; 
     NSInteger extrahierteBildBreite =[nsImageRep pixelsWide]; 
     NSNumber* bildBreite = [NSNumber numberWithInt:extrahierteBildBreite]; 
     Bild* bild = [[Bild alloc] init]; 
     [bild setVollstaendigerBildName:bildPfad]; 
     if ((bildHoehe == self.erwarteteBildHoehe) && (bildBreite == self.erwarteteBildBreite)) { 
      [bild setBildformat:hochformat]; 
     } else if ((bildHoehe == self.erwarteteBildBreite) && (bildBreite == self.erwarteteBildHoehe)) { 
      [bild setBildformat:querformat]; 
     } else if (bildHoehe ==bildBreite){ 
      [bild setBildformat:quadratisch]; 
     } else { 
      continue; 
     } 
    [neueBildObjektListe insertObject:bild atIndex:i]; 
    } 
    self.bildObjektListe = neueBildObjektListe; 
} 
@end 

与类“图片报”:

//Bild.h:

#import <Cocoa/Cocoa.h> 
#import <Foundation/Foundation.h> 

typedef enum {hochformat, quadratisch, querformat} Bildformat; 

@interface Bild : NSObject 
@property (assign) NSString *vollstaendigerBildName; 
@property (assign) Bildformat bildformat; 

- (Bild*)init; 
- (Bild*)init:(NSString *) bildName nunDasBildformat:(Bildformat)dasBildFormat; 
- (void)setVollstaendigerBildName:(NSString *) neuerVollstaendigerBildName; 
- (NSString *)vollstaendigerBildName; 
- (void)setBildformat:(Bildformat) neuesBildformat; 
- (Bildformat)bildformat; 
@end 

// Bild。米:

#import "Bild.h" 

@implementation Bild 

- (Bild*)init { 
    self = [super init]; 
    if (self){ 
     self.vollstaendigerBildName = @""; 
     self.bildformat = hochformat; 
    } 
    return self; 
} 

/*Der explizite Konstruktor dieser Klasse. 
Parameter NSString bildName 
Parameter Bildformat dasBildFormat 
Rueckgabe Bild bild*/ 
- (Bild*)init:(NSString *)bildName nunDasBildformat:(Bildformat)dasBildFormat { 
    self = [super init]; 
    if (self){ 
     self.vollstaendigerBildName = bildName; 
     self.bildformat = dasBildFormat; 
    } 
    return self; 
} 

- (void) setVollstaendigerBildName:(NSString *) neuerVollstaendigerBildName{ 
    self.vollstaendigerBildName = neuerVollstaendigerBildName; 
} 

- (NSString *) vollstaendigerBildName{ 
    return self.vollstaendigerBildName; 
} 

- (void)setBildformat:(Bildformat) neuesBildformat{ 
    self.bildformat = neuesBildformat; 
} 

- (Bildformat)bildformat{ 
    return self.bildformat; 
} 
@end 

的代码显示在编辑器中没有编译错误,但也有在集结结果错误:错误消息:

Ld build/Debug/FotobuchErsteller_0 normal x86_64 
cd /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 
setenv MACOSX_DEPLOYMENT_TARGET 10.6 
/Developer/usr/bin/gcc-4.2 -arch x86_64 
-isysroot /Developer/SDKs/MacOSX10.6.sdk   -L/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug  
-F/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug 
-filelist /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/Objects-normal/x86_64/FotobuchErsteller_0.LinkFileList -mmacosx-version-min=10.6 
-framework Foundation -o /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug/FotobuchErsteller_0 

Undefined symbols: 
    "_OBJC_CLASS_$_NSBitmapImageRep", referenced from: 
     objc-class-ref-to-NSBitmapImageRep in BildAnalyst.o 
ld: symbol(s) not found 
collect2: ld returned 1 exit status 

然后我试图用相同的以下类“BildAnalyst”:

我用

NSURL* nsUrl = [NSURL fileURLWithPath:bildPfad]; 
CIImage* nsImageRep = [CIImage imageWithContentsOfURL:nsUrl]; 

,而不是

NSBitmapImageRep* nsImageRep = [NSBitmapImageRep imageRepWithContentsOfFile:bildPfad]; 

的代码显示在编辑器中没有编译错误,但也有在集结结果错误:错误消息:

Ld build/Debug/FotobuchErsteller_0 normal x86_64 
cd /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 
setenv MACOSX_DEPLOYMENT_TARGET 10.6 
/Developer/usr/bin/gcc-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk  -L/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug  -F/Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug -filelist /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/Objects-normal/x86_64/FotobuchErsteller_0.LinkFileList -mmacosx-version-min=10.6 -framework Foundation -o /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/Debug/FotobuchErsteller_0 

Undefined symbols: 

"_OBJC_CLASS_$_CIImage", referenced from: 
    objc-class-ref-to-CIImage in BildAnalyst.o 
ld: symbol(s) not found 
collect2: ld returned 1 exit status 

然后,我尝试用相同的类下面的“ BildAnalyst“:

我用

NSURL* nsUrl = [NSURL fileURLWithPath:bildPfad]; 
NSData* nsData = [NSData dataWithContentsOfURL:nsUrl]; 
UIImage* nsImageRep = [UIImage imageWithData:nsData]; 

,而不是

NSBitmapImageRep* nsImageRep = [NSBitmapImageRep imageRepWithContentsOfFile:bildPfad]; 

该代码在UIImage行显示编译错误。差错消息:

CompileC build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/Objects-normal/x86_64/BildAnalyst.o BildAnalyst.m normal x86_64 objective-c com.apple.compilers.gcc.4_2 
cd /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 
setenv LANG en_US.US-ASCII 

/Developer/usr/bin/gcc-4.2 -x目标c -arch x86_64的-fmessage长度= 0 -pipe -std = gnu99 -Wno-三合字母-fpascal-字符串-fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Developer/SDKs/MacOSX10.6.sdk -mfix-and-continue -mmacosx-version -min = 10.6 -gdwarf-2
-iquote /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /建造/ FotobuchErsteller_0.build /调试/ FotobuchErsteller_0.build/FotobuchErsteller_0生成-files.hmap
-I /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /建造/ FotobuchErsteller_0.build/Debug/Fotobuch Ersteller_0.build/FotobuchErsteller_0-own-target-headers.hmap
-I /用户/江户/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /编译/ FotobuchErsteller_0.build /调试/ FotobuchErsteller_0.build/FotobuchErsteller_0-所有目标报头。 HMAP -iquote /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/FotobuchErsteller_0-project-headers.hmap
-F /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /建造/调试
-I /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /建造/调试/包括
-I /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller /图片buchErsteller_0 /建造/ FotobuchErsteller_0.build /调试/ FotobuchErsteller_0.build/DerivedSources/x86_64的
-I /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0 /建造/ FotobuchErsteller_0.build /调试/ FotobuchErsteller_0.build/DerivedSources -include/VAR /文件夹/ 1J/1J8zfhnuH3Gk3T ++ +++ j4sPBE TI/-Caches-/com.apple.Xcode.501/SharedPrecompiledHeaders/FotobuchErsteller_0_Prefix-gqjmlqmlzpuhpyfvtxjjyzqdlikg/FotobuchErsteller_0_Prefix.pch -c /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst。米 -o /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/build/FotobuchErsteller_0.build/Debug/FotobuchErsteller_0.build/Objects-normal/x86_64/BildAnalyst.o /用户/江户时代/文档/ FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:在函数 ' - [BildAnalyst analysiereBilderListe]': /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:112:错误: '的UIImage' 未申报(第一在此函数使用) /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:112:错误:(每个未声明的标识符仅报告一次 /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:112:错误:它出现在每个函数中。) /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:112:错误: 'nsImageRep' 未声明(在一次使用此功能) /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m :113:警告:未使用的变量 'bildHoehe' /Users/Edo/Documents/FotobuchSkriptErsteller/FotobuchSkriptErsteller/FotobuchErsteller_0/BildAnalyst.m:104:警告: 未使用的变量 'neueBildObjektListe' {标准输入}:未知:未定义局部符号L_OBJC_CLASSLIST_SUP_REFS_ $ _0 {标准输入}:未知:未定义的本地符号L_OBJC_SELECTOR_REFERENCES_0 {标准输入}:未知:未定义的本地符号L_OBJC_SELECTOR_REFERENCES_1 {标准输入}:未知:未定义的本地符号ymbol L_OBJC_CLASSLIST_REFERENCES _ $ _ 1 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_2 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_3 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_4 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_5 {标准输入}:未知:未定义局部符号L_OBJC_SELECTOR_REFERENCES_6 {标准输入}:未知:未定义局部符号L_OBJC_SELECTOR_REFERENCES_7 {标准输入}:未知:未定义局部符号L_OBJC_SELECTOR_REFERENCES_8 {标准输入}:未知:未定义局部符号L_OBJC_SELECTOR_REFERENCES_9 {标准输入}:未知:未定义的本地符号L _OBJC_CLASSLIST_REFERENCES _ $ _ 2 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_10 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_11 {标准输入}:未知:未定义本地符号L_OBJC_SELECTOR_REFERENCES_12

大家有一个建议,就是我可以改善或者可能是另一种方式我只想将保存在本地硬盘驱动器“Macintosh HD”上的图片的宽度和高度设置为数字(int,NSInteger,NSNumber,...)。

回答

0

命令行工具默认情况下仅链接到Foundation。您需要确保您的项目与AppKit框架(或Cocoa,它自动链接到Foundation或AppKit)链接。在Xcode中选择你的项目,选择你的目标,然后点击“Build Phases”标签。在“与库链接二进制文件”中,添加AppKit。您应该有权访问AppKit类,例如NSImage

+0

非常感谢!依赖关系现在可用!如果有人以确切的方式搜索,如何添加AppKit,下面是我发现的内容:点击“目标” - ><应用程序名称> - >“链接二进制库”中的“概述”,然后点击“操作” - - >“添加” - >“现有框架” - >“AppKit.framework”,条件为“全部”已在限制 - >“添加”中标记。 – ebAtObjectiveC

+0

也许,你可以添加,如何发现,NSImage是AppKit的一部分。这只是因为我现在正在寻找类似的方式来导入用于编写UnitTests的“”,我不知道如何将它与特定的框架关联。 – ebAtObjectiveC

+0

每个课程的文档都在顶部说明了它来自哪个框架。如果你看看'NSImage'的文档:https://developer.apple.com/documentation/appkit/nsimage并滚动到顶部,你可以在标题中看到它。 –

相关问题