3 回答
TA贡献1831条经验 获得超10个赞
您的代码不起作用的原因是因为没有考虑您所拥有的代码上的imageOrientation。具体来说,如果imageOrientation为右/左,则需要同时旋转图像和交换宽度/高度。这是执行此操作的一些代码:
-(UIImage*)imageByScalingToSize:(CGSize)targetSize
{
UIImage* sourceImage = self;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
这将调整图像的大小并将其旋转到正确的方向。如果需要弧度的定义,则为:
static inline double radians (double degrees) {return degrees * M_PI/180;}
Daniel给出的答案也是正确的,但是由于您使用的是UIGraphicsBeginImageContext(),因此它存在线程不安全的问题。由于上述代码仅使用CG功能,因此已全部准备就绪。我还有一个类似的功能,可以调整大小并在图像上进行适当的纵横比填充-让我知道这是否是您要的内容。
TA贡献1851条经验 获得超5个赞
我那里的一些代码也遇到了这个问题,我发现此代码有效,请检查一下,让我知道您找到了什么
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
TA贡献1883条经验 获得超3个赞
如果要通过在矩形图像(水平或垂直)中通过在两侧的宽度或高度进行裁剪来制作正方形图像,请使用我的代码。所不同的是,我不会伸展,但会收割。我通过修改从最上面的代码中获得了它:
//Cropping _image to fit it to the square by height or width
CGFloat a = _image.size.height;
CGFloat b = _image.size.width;
CGRect cropRect;
if (!(a==b)) {
if (a<b) {
cropRect = CGRectMake((b-a)/2.0, 0, a, a);
b = a;
}
else if (b<a) {
cropRect = CGRectMake(0, (a-b)/2.0, b, b);
a = b;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect);
UIImage* sourceImage = _image;
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -a);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -a, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, a, a);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
_image = [UIImage imageWithCGImage:ref];
CGImageRelease(imageRef);
}
- 3 回答
- 0 关注
- 532 浏览
添加回答
举报