旋转后保存图像。但是图片保存后分辨率变低
Save image after rotate it. But picture has lower resolution after saving
我正在尝试从 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(Retina,13 英寸,2014 年中)
非常感谢您:)
我认为你把图片旋转了两次。
方法内部rotateLeft
//First rotation
//Rotate Picture
Img = [self imageRotatedByDegrees:Img and:90];
//Second rotation
[_imageView rotateImageLeft:sender];
所以我相信你需要阻止第二次轮换,你会解决你的问题。
我正在尝试从 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(Retina,13 英寸,2014 年中)
非常感谢您:)
我认为你把图片旋转了两次。
方法内部rotateLeft
//First rotation
//Rotate Picture
Img = [self imageRotatedByDegrees:Img and:90];
//Second rotation
[_imageView rotateImageLeft:sender];
所以我相信你需要阻止第二次轮换,你会解决你的问题。