2014-03-01 5 views
0

rotating에 문제가 있으며 JPEGNSImage으로 저장하는 중입니다.NSView에서 이미지 저장

- (NSImage*)imageRotatedByDegrees:(CGFloat)degrees 
{ 
    // calculate the bounds for the rotated image 
    NSRect imageBounds = {NSZeroPoint, [image size]}; 
    NSBezierPath* boundsPath = [NSBezierPath 
          bezierPathWithRect:imageBounds]; 
    NSAffineTransform* transform = [NSAffineTransform transform]; 

    [transform rotateByDegrees:degrees]; 
    [boundsPath transformUsingAffineTransform:transform]; 

    NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size}; 
    NSImage* rotatedImage = [[NSImage alloc] 
         initWithSize:rotatedBounds.size]; 

    // center the image within the rotated bounds 
    imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth 
               (imageBounds)/2); 
    imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight 
               (imageBounds)/2); 

    // set up the rotation transform 
    transform = [NSAffineTransform transform]; 
    [transform translateXBy:+(NSWidth(rotatedBounds)/2) yBy:+ 
    (NSHeight(rotatedBounds)/2)]; 
    [transform rotateByDegrees:degrees]; 
    [transform translateXBy:-(NSWidth(rotatedBounds)/2) yBy:- 
    (NSHeight(rotatedBounds)/2)]; 

    // draw the original image, rotated, into the new image 
    [rotatedImage lockFocus]; 
    [transform set]; 
    [image drawInRect:imageBounds fromRect:NSZeroRect 
    operation:NSCompositeCopy fraction:1.0] ; 
    [rotatedImage unlockFocus]; 

    return rotatedImage; 
} 

이미지가 성공적으로 회전 : 나는

- (BOOL)isFlipped 
{ 
    return YES; 
} 

뒤집어 NSView 그럼 내가 다음과 같은 기능을 NSImage 회전을 적용하고 있습니다 있습니다. 나는 다음 코드를 사용하여 JPEG를 저장하기 위해 노력하고있어 때 나중에 : JPEG 파일이 잘못 뒤집어

-(void)saveDocument:(id)sender 
{ 
    NSData *imageData = [image TIFFRepresentation]; 
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 
    NSDictionary *imageProps = 
    [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] 
        forKey:NSImageCompressionFactor]; 
    imageData = [imageRep representationUsingType:NSJPEGFileType 
            properties:imageProps]; 
    [imageData writeToFile:[_imageURL path] atomically:YES]; 
} 

결과 ... 내가 잘못 뭐하는거야?

감사 아이디어에 대한 많은, 페트르

답변

0

마지막으로 올바른 해결책을 찾았습니다. 내가 이미지를 저장있을 때

- (BOOL)isFlipped 
{ 
    return NO; 
} 

그런 다음이 설정 NSImage하는 것이 중요합니다 (감사 pointum하는!)

[_image lockFocusFlipped:YES]; 

를 지금부터, 그는 제대로 회전 및 대칭 이동합니다.

0

- [NSImage lockFocusFlipped :] 도움을 줄 수.

+0

[NSImage lockFocusFlipped :]를 사용하면 결과가 동일합니다. – Jodynek