2014-03-01 50 views
0

我遇到問題rotating並保存JPEGNSImage。我有NSView其翻轉將圖像保存在NSView中

- (BOOL)isFlipped 
{ 
    return YES; 
} 

然後我申請NSImage旋轉與下面的函數:

- (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; 
} 

圖片現已成功旋轉。後來,當我試圖保存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]; 
} 

結果JPEG文件被錯誤地翻轉......我做錯了嗎?

非常感謝您的任何想法,切赫

回答

0

最後,我找到了正確的解決方案。

- (BOOL)isFlipped 
{ 
    return NO; 
} 

然後是設置NSImage中重要的(感謝pointum!)

[_image lockFocusFlipped:YES]; 

從現在開始,當我保存圖像,他是正確的旋轉和翻轉。

0

- [NSImage中lockFocusFlipped:]能有所幫助。

+0

不幸的是,當使用[NSImage lockFocusFlipped:]結果相同。 – Jodynek