Dix*_*ine 1 iphone objective-c rotation uiimage
我正在使用此代码旋转UIImage。
CGFloat DegreesToRads(CGFloat degrees) {
return degrees * M_PI / 180;
}
- (UIImage *)scaleAndRotateImage:(UIImage *)image forAngle: (double) angle {
float radians=DegreesToRads(angle);
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
//Rotate the image context
CGContextRotateCTM(bitmap, radians);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2 , image.size.width, image.size.height), image.CGImage );
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Run Code Online (Sandbox Code Playgroud)
正常,但旋转后图像质量较差。可能是什么原因?
尝试
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 2.0)
Run Code Online (Sandbox Code Playgroud)
从苹果文档:
void UIGraphicsBeginImageContextWithOptions(
CGSize size,
BOOL opaque,
CGFloat scale
);
Run Code Online (Sandbox Code Playgroud)
参量
size新位图上下文的大小(以磅为单位)。这表示UIGraphicsGetImageFromCurrentImageContext函数返回的图像的大小。若要获取位图的大小(以像素为单位),必须将width和height值乘以scale参数中的值。
opaque 一个布尔型标志,指示位图是否不透明。如果知道位图是完全不透明的,请指定“是”以忽略Alpha通道并优化位图的存储。指定“否”意味着位图必须包含一个alpha通道以处理任何部分透明的像素。
再见:)
| 归档时间: |
|
| 查看次数: |
628 次 |
| 最近记录: |