2017-08-07 158 views
0

我想从IKImageView旋转图像。但是在旋转并保存之后,我得到了较低分辨率的图片。
下面是我使用用于旋转旋转后保存图像。但保存后图片的分辨率降低

-(NSImage *)imageRotated:(float)degrees{ 
    degrees = fmod(degrees, 360.); 
    if(0 == degrees){ 
     return self; 
    } 
    NSSize size = [self size]; 
    NSSize maxSize; 
    if(90. == degrees || 270. == degrees || -90== degrees || -270. == degrees){ 
     maxSize = NSMakeSize(size.height, size.width); 
    }else if (180. == degrees || -180. == degrees){ 
     maxSize = size; 
    }else{ 
     maxSize = NSMakeSize(20+MAX(size.width, size.height), 20+MAX(size.width, size.height)); 
    } 

    NSAffineTransform *rot = [NSAffineTransform transform]; 
    [rot rotateByDegrees:degrees]; 
    NSAffineTransform *center = [ NSAffineTransform transform]; 
    [center translateXBy:maxSize.width/2 yBy:maxSize.height/2]; 
    [rot appendTransform:center]; 
    NSImage *image = [[NSImage alloc]init]; 
    image = [NSImage imageWithSize:maxSize flipped:NO drawingHandler:^BOOL(NSRect desRect){ 
      [rot concat]; 
      NSRect rect = NSMakeRect(0, 0, size.width, size.height); 
      NSPoint corner = NSMakePoint(-size.width/2, -size.height/2); 
      [self drawAtPoint:corner fromRect:rect operation:NSCompositeCopy fraction:1.0]; 
     return YES; 
    }]; 
    return image; 
} 


然后代码,我转换图像通过下面的代码

- (NSBitmapImageRep *)bitmapImageRepresentation{ 
    int width = [self size].width; 
    int height = [self size].height; 

    if(width <1 || height < 1) 
     return nil; 

    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] 
          initWithBitmapDataPlanes:NULL 
          pixelsWide:width 
          pixelsHigh:height 
          bitsPerSample:8 
          samplesPerPixel:4 
          hasAlpha:YES 
          isPlanar:NO 
          colorSpaceName:NSDeviceRGBColorSpace 
          bytesPerRow:width*4 
          bitsPerPixel:32]; 

    NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:rep]; 
    [NSGraphicsContext saveGraphicsState]; 
    [NSGraphicsContext setCurrentContext:ctx]; 
    [self drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; 
    [ctx flushGraphics]; 
    [NSGraphicsContext restoreGraphicsState]; 
    return rep; 
} 


成位图和我用下面的代码转换为NSData的,然后写文件

- (NSData*)toNSData{ 
    NSImage *imgTemp = [[NSImage alloc]initWithSize:NSMakeSize(self.size.width*2, self.size.height*2)]; 
    imgTemp = self; 
    NSBitmapImageRep *bmprep = [imgTemp bitmapImageRepresentation]; 
    NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCurrentFrame]; 
    NSData *pngData = [[NSData alloc]init]; 
    pngData = [bmprep representationUsingType:NSPNGFileType properties:imageProps]; 
    return pngData; 
} 

你能告诉我为什么图片保存后有更低的分辨率?
我使用的MacBook Pro(视网膜,13英寸,中秋节2014)
非常感谢你提前:)

+0

问题是什么?保存后的图像被旋转。这不是你想要的吗? – FightOn

+0

@FightOn:您好,非常感谢您的反馈。我再次更新了我的问题。你能帮我再检查一次吗? – NextTrang

回答

0

我想你两次旋转图像。

里面的方法rotateLeft

//First rotation 
//Rotate Picture 
     Img = [self imageRotatedByDegrees:Img and:90]; 

//Second rotation 
[_imageView rotateImageLeft:sender]; 

所以,我相信你会需要阻止第二旋转,你会摆脱你的问题。

+0

谢谢你的回答,这对我来说非常有用。目前,我面临着另一个问题。这是保存后图像的分辨率下降。我更新了我的问题,你能帮我再检查一次 – NextTrang

+0

看起来在你的更新问题中,它与我记忆中的代码完全不同,所以我建议你单独提问以获得更多人的帮助。如果它解决了您的问题,也不要忘记接受并提出答案,这有助于我们更好地理解问题。谢谢 –