2012-07-05 36 views
5

Tôi muốn thêm thánh giá trên NSImage, đây là mã của tôi:NSImage và võng mạc trưng bày nhầm lẫn

-(NSSize)convertPixelSizeToPointSize:(NSSize)px 
{ 
    CGFloat displayScale = [[NSScreen mainScreen] backingScaleFactor]; 
    NSSize res; 
    res.width = px.width/displayScale; 
    res.height = px.height/displayScale; 
    return res; 
} 

-(void)awakeFromNib 
{ 
    CGFloat scale = [[NSScreen mainScreen] backingScaleFactor]; 

    NSLog(@"backingScaleFactor : %f",scale); 

    NSImage *img = [[[NSImage alloc]initWithContentsOfFile:@"/Users/support/Pictures/cat.JPG"] autorelease]; 


    NSBitmapImageRep *imgRep = [NSBitmapImageRep imageRepWithData:[img TIFFRepresentation]]; 
    NSSize imgPixelSize = NSMakeSize([imgRep pixelsWide],[imgRep pixelsHigh]); 
    NSSize imgPointSize = [self convertPixelSizeToPointSize:imgPixelSize]; 
    [img setSize:imgPointSize]; 

    NSLog(@"imgPixelSize.width: %f , imgPixelSize.height:%f",imgPixelSize.width,imgPixelSize.height); 
    NSLog(@"imgPointSize.width: %f , imgPointSize.height:%f",imgPointSize.width,imgPointSize.height); 

    [img lockFocus]; 
    NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease]; 
    [trans scaleBy:1.0/scale]; 
    [trans set]; 

    NSBezierPath *path = [NSBezierPath bezierPath];  
    [[NSColor redColor] setStroke]; 
    [path moveToPoint:NSMakePoint(0.0, 0.0)]; 
    [path lineToPoint:NSMakePoint(imgPixelSize.width, imgPixelSize.height)]; 
    [path moveToPoint:NSMakePoint(0.0, imgPixelSize.height)]; 
    [path lineToPoint:NSMakePoint(imgPixelSize.width, 0.0)]; 

    [path setLineWidth:1]; 
    [path stroke]; 
    [img unlockFocus]; 

    [imageView setImage:img]; 

    imgRep = [NSBitmapImageRep imageRepWithData:[img TIFFRepresentation]]; 
    NSData *imageData = [imgRep representationUsingType:NSJPEGFileType properties:nil]; 
    [imageData writeToFile:@"/Users/support/Pictures/11-5.JPG" atomically:NO]; 
} 

trên phi võng mạc hiển thị kết quả là: enter image description here

và giao diện điều khiển hiển thị:

2012-07-06 00:53:09.889 RetinaTest[8074:403] backingScaleFactor : 1.000000 
2012-07-06 00:53:09.901 RetinaTest[8074:403] imgPixelSize.width: 515.000000 , imgPixelSize.height:600.000000 
2012-07-06 00:53:09.902 RetinaTest[8074:403] imgPointSize.width: 515.000000 , imgPointSize.height:600.000000 

nhưng trên màn hình hiển thị võng mạc (Tôi không sử dụng màn hình võng mạc thực nhưng chế độ hidpi): enter image description here

console:

2012-07-06 00:56:05.071 RetinaTest[8113:403] backingScaleFactor : 2.000000 
2012-07-06 00:56:05.083 RetinaTest[8113:403] imgPixelSize.width: 515.000000 , imgPixelSize.height:600.000000 
2012-07-06 00:56:05.084 RetinaTest[8113:403] imgPointSize.width: 257.500000 , imgPointSize.height:300.000000 

Nếu tôi bỏ qua những dòng này:

NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease]; 
      [trans scaleBy:1.0/scale]; 
      [trans set]; 

enter image description here

Tuy nhiên nếu tôi thay đổi [NSAffineTransform scaleBy] đến 1.0 kết quả là đúng

NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease]; 
     [trans scaleBy:1.0]; 
     [trans set]; 

enter image description here

Console:

2012-07-06 01:01:03.420 RetinaTest[8126:403] backingScaleFactor : 2.000000 
2012-07-06 01:01:03.431 RetinaTest[8126:403] imgPixelSize.width: 515.000000 , imgPixelSize.height:600.000000 
2012-07-06 01:01:03.432 RetinaTest[8126:403] imgPointSize.width: 257.500000 , imgPointSize.height:300.000000 

bất cứ ai có thể đưa ra một lời giải thích được không? chế độ hidpi khác với màn hình võng mạc?

Trả lời

4

Tôi nghĩ rằng tôi đã tìm thấy câu trả lời. Nếu NSAffineTransform được đặt thành ngữ cảnh của NSImage, nó sẽ biến đổi hệ tọa độ thành kích thước pixel, là kích thước 2 x điểm. Ngay cả khi nó trống như thế này:

NSAffineTransform *trans = [[[NSAffineTransform alloc] init] autorelease]; 
[trans set]; 

Tôi không biết nếu đó là lỗi hay đó là cách hoạt động của nó.

0

Đặt lại chuyển đổi không có lỗi (đề cập đến câu trả lời của riêng bạn). Sử dụng hidpi làm cho biến đổi mặc định là một biến cho phép hầu hết các mã mức cao hoạt động tốt như hiện trạng. Đặt lại để chuyển đổi danh tính hoàn tác việc này để căn chỉnh hệ tọa độ 1: 1 với pixel.

Một điều hiếm khi cần phải thực hiện điều này. Đơn giản hóa mã của bạn để đưa ra các đại diện hình ảnh và chuyển đổi cho phép bạn có được điều này:

NSImage *img = [[[NSImage alloc]initWithContentsOfFile:@"/Users/support/Pictures/cat.JPG"] autorelease]; 
NSSize size = img.size; 

NSBezierPath *path = [NSBezierPath bezierPath];  
[[NSColor redColor] setStroke]; 
[path moveToPoint:NSMakePoint(0.0, 0.0)]; 
[path lineToPoint:NSMakePoint(size.width, size.height)]; 
[path moveToPoint:NSMakePoint(0.0, size.height)]; 
[path lineToPoint:NSMakePoint(size.width, 0.0)]; 

[path setLineWidth:1]; 
[path stroke]; 
[img unlockFocus]; 

[imageView setImage:img]; 
... 

này nên làm việc ngoại trừ dòng thay vào đó sẽ là 2 pixels vật lý rộng (nhưng tất nhiên tương tự như chiều rộng để các hình ảnh trên một thường xuyên màn). Hãy thử trường hợp đặc biệt đơn giản hơn này để khắc phục điều đó:

[path setLineWidth:(img.scale > 1) ? 0.5 : 1.0];