2012-11-29 5 views
4

큰 이미지를 자르고 다양한 이미지로 저장 중입니다. 먼저 iOS에서이 코드를 구현했지만 정상적으로 작동하지만 코드를 OSX으로 포팅하면 이미지의 오른쪽과 오른쪽에 얇은 흰색 선 (1 픽셀)이 나타납니다. 이 선은 순수한 흰색 또는 단색이 아닙니다 (아래 샘플 참조).이미지를자를 때 얇은 흰색 선이 추가됨 (Objective-C OSX)

NSImage *source = [[[NSImage alloc]initWithContentsOfFile:imagePath] autorelease]; 
//init the image 
NSImage *target = [[[NSImage alloc]initWithSize:panelRect.size] autorelease]; 
//start drawing 
[target lockFocus]; 
[source drawInRect:NSMakeRect(0,0,panelRect.size.width,panelRect.size.height) 
      fromRect:NSMakeRect(panelRect.origin.x , source.size.height - panelRect.origin.y - panelRect.size.height, panelRect.size.width, panelRect.size.height) 
     operation:NSCompositeCopy 
      fraction:1.0]; 
[target unlockFocus]; 

//create a NSBitmapImageRep 
NSBitmapImageRep *bmpImageRep = [[[NSBitmapImageRep alloc]initWithData:[target TIFFRepresentation]] autorelease]; 
//write to tiff 
[[target TIFFRepresentation] writeToFile:@"outputImage.tiff" atomically:NO]; 
[target addRepresentation:bmpImageRep];  
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]; 

//get the data from the representation 
NSData *data = [bmpImageRep representationUsingType: NSJPEGFileType 
              properties: imageProps]; 

//write the data to a file 
[data writeToFile: @"outputImage.jpg" atomically:NO]; 

data = [bmpImageRep representationUsingType: NSPNGFileType properties: imageProps]; 

//write the data to png 
[data writeToFile: @"outputImage.png" atomically:NO]; 
: 여기
-(void)testMethod:(int)page forRect:(CGRect)rect{ 
    NSString *filePath = @"imageName"; 

    NSData *data = [HeavyResourceManager dataForPath:filePath];//this just gets the image as NSData 
    UIImage *image = [UIImage imageWithData:data]; 

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);//crop in the rect 

    UIImage *result = [UIImage imageWithCGImage:imageRef scale:0 orientation:image.imageOrientation]; 
    CGImageRelease(imageRef); 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

    [UIImageJPEGRepresentation(result, 1.0) writeToFile:[documentsDirectoryPath stringByAppendingPathComponent::@"output.jpg"] atomically:YES]; 
} 

이 흰색 선이 추가되도록 OSX에 이식 된 코드이다 : 여기

는 챔피언처럼 작동 하나의 서브 - 이미지를 만들기 위해 iOS 코드이며

위의 코드는 이미지가 세 가지 형식으로 저장되어 문제가 특정 형식의 저장 프로세스에 있는지 확인합니다. 모든 형식에 동일한 문제가있어 보이지 않습니다. 여기

가 날려 이미지의 우측 상단의 (4 배) 버전된다.

OSX, note the white line top and left

(OSX는 흰색 라인 최고를 기록하고 왼쪽이 있기 때문에, 여기에 흐림처럼 보인다 이미지가 THER


iOS, note no white lines

(아이폰 OS는주의)를 불어 e는 흰 줄이 아님)

누군가 이런 일이 일어날 수있는 이유를 말할 수 있다면 매우 행복 할 것입니다. 아마도 품질 차이와 관련이 있습니다 (OSX 버전은 품질이 낮아 보이지만 눈치 채실 수는 없지만). 아마도 이것을 수행하는 완전히 다른 방법이있을 것입니다.

OSX Image original


업데이트 : 참고로

가 여기에 스케일 없음의 OSX 이미지입니다

//start drawing on target 
    [target lockFocus]; 
    [NSGraphicsContext saveGraphicsState]; 
    [[NSGraphicsContext currentContext] 
      setImageInterpolation:NSImageInterpolationNone]; 
    [[NSGraphicsContext currentContext] setShouldAntialias:NO]; 

    //draw the portion of the source image on target image 
    [source drawInRect:NSMakeRect(0,0,panelRect.size.width,panelRect.size.height) 
       fromRect:NSMakeRect(panelRect.origin.x , source.size.height - panelRect.origin.y - panelRect.size.height, panelRect.size.width, panelRect.size.height) 
      operation:NSCompositeDestinationAtop 
       fraction:1.0]; 

    [NSGraphicsContext restoreGraphicsState]; 
    //end drawing 
    [target unlockFocus]; 
: Daij-Djan 덕분에, 나는 안티 앨리어싱에서 drawInRect 방법을 중지 할 수 있었다

업데이트 : '보간법'이 NSImageInterpolationNone으로 변경되었습니다. 더 나은 표현이 가능합니다. 높은 보간은 사소한 조정을하며, 텍스트를 확대 할 때 눈에.니다. 보간을 제거하면 픽셀이 튀는 것을 막을 수 있지만 여전히 색상에는 약간의 차이가 있습니다 (회색 색상의 경우 164 ~ 155). iOS에서 할 수있는 것처럼 이미지를자를 수있어서 좋을 것입니다 ...

답변

4

안티 앨리어싱처럼 보입니다 ... 이미지를 자르거나 스케일링 할 때 계산 한 부동 소수점 값을 반올림해야합니다.

사용 froundf() 플로트에이

+1

++++ 1 값 -이 앤티 엘리 어싱이었다! 감사! –

+0

참고 : 안티 앨리어싱을 중지했습니다 (위의 편집 참조). –