// 561 is PID of Chrome application in Activity Monitor.
NSRunningApplication *chromeApp = [NSRunningApplication runningApplicationWithProcessIdentifier:561];
self.imgView.image = chromeApp.icon;
我的日志:
(lldb) po chromeApp.icon
<NSImage 0x608000268c40 Size={32, 32} Reps=(
"<NSIconRefImageRep:0x600000082df0 iconRef=0x103 size:128x128 pixels:128x128>",
"<NSIconRefImageRep:0x600000083020 iconRef=0x103 size:128x128 pixels:256x256>",
"<NSIconRefImageRep:0x600000082fd0 iconRef=0x103 size:256x256 pixels:256x256>",
"<NSIconRefImageRep:0x600000083070 iconRef=0x103 size:256x256 pixels:512x512>",
"<NSIconRefImageRep:0x6000000830c0 iconRef=0x103 size:512x512 pixels:512x512>",
"<NSIconRefImageRep:0x600000083110 iconRef=0x103 size:48x48 pixels:48x48>",
"<NSIconRefImageRep:0x600000083160 iconRef=0x103 size:36x36 pixels:36x36>",
"<NSIconRefImageRep:0x600000083200 iconRef=0x103 size:36x36 pixels:72x72>",
"<NSIconRefImageRep:0x600000083250 iconRef=0x103 size:32x32 pixels:32x32>",
"<NSIconRefImageRep:0x6000000832a0 iconRef=0x103 size:32x32 pixels:64x64>",
"<NSIconRefImageRep:0x6000000832f0 iconRef=0x103 size:18x18 pixels:18x18>",
"<NSIconRefImageRep:0x6000000831b0 iconRef=0x103 size:18x18 pixels:36x36>",
"<NSIconRefImageRep:0x600000083340 iconRef=0x103 size:16x16 pixels:16x16>",
"<NSIconRefImageRep:0x600000083390 iconRef=0x103 size:16x16 pixels:32x32>",
"<NSIconRefImageRep:0x6000000833e0 iconRef=0x103 size:512x512 pixels:1024x1024>"
)>
在上面的代码中,我收到一张大小为32x32的图像(在我的xib文件中,imageView&#39;大小为512 x 512)。 问题:如何使用512x512像素获取图像。
答案 0 :(得分:3)
您获得的图标是一个NSImage,其ITS大小设置为32x32。意味着如果你现在画它,它会选择一个合适的NSImageRepresentation。从日志中你可以看到它有许多不同的 - 它们是持久数据
所以你需要做的就是:
self.imgView.image.size = NSMakeSize(512,512);
NSImage基本上只是一个'可绘制',它没有自己的数据,只有1'个图像数据的表示,它“组合在一起”
答案 1 :(得分:1)
在这个答案中,我尝试使用Safari(而不是Chrome),因为我不知道Chrome的bundleIdentifier。以下代码包含一些用于测试的NSLog,但没有错误处理和内存管理:
获取NSRunningApplication:
NSArray *appArray =
[NSRunningApplication
runningApplicationsWithBundleIdentifier:@"com.apple.Safari"];
NSLog(@"%@", appArray );
NSRunningApplication *safariApp = [appArray objectAtIndex:0];
首先加载应用程序的图标:
NSImage *iconImage = safariApp.icon;
NSLog( @"iconImage:\n%@", ); //show all representations
然后提取最适合您的NSImageView的NSImageRep:
NSRect rect = [myImageView frame];
NSLog( @"rect = %@", NSStringFromRect( rect ) );
NSImageRep *rep = [iconImage bestRepresentationForRect:rect
context:nil
hints:nil];
NSLog( @"rep = %@", ); // what rep is selected?
// make an NSImage from this rep
NSImage *theImage = [[NSImage alloc] initWithSize:NSZeroSize];
[theImage addRepresentation:rep];
self.imgView.image = theImage;
在您的示例中(使用sizeView = 512x512的imageView),将选择大小为= 512x512像素= 512x512的rep。如果你有一个视网膜屏幕,你会得到尺寸= 512x512像素= 1024x1024的代表。