我遇到rotating
问题并保存JPEG
NSImage
。我翻了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
文件被错误翻转......我做错了什么?
非常感谢任何想法,Petr
答案 0 :(得分:0)
- [NSImage lockFocusFlipped:]可以提供帮助。
答案 1 :(得分:0)
最后,我确实找到了正确的解决方案。
- (BOOL)isFlipped
{
return NO;
}
然后设置NSImage非常重要(感谢pointum!)
[_image lockFocusFlipped:YES];
从现在开始,当我保存图像时,他被正确旋转并翻转。