【发布时间】:2011-09-13 13:34:03
【问题描述】:
我有一个宽度为 75 和高度为 75 的 UIImageview 视图。我从相机或照片库中捕获图像,调整它的大小并设置为 imageview。
捕获的图像上传到服务器,但上传调整大小的图像,图像质量丢失。从服务器检索时查看模糊图像。
现在,我需要的是,每当我点击 imageview 时,应该会显示全屏图像预览,并且应该上传高质量的图像而不是调整大小的图像
我使用以下代码调整了图像大小
- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSize:(CGSize)newSize;
{
CGFloat targetWidth = newSize.width;
CGFloat targetHeight = newSize.height;
NSLog(@"target Width:%f",targetWidth);
NSLog(@"target Height:%f",targetHeight);
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaPremultipliedLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
NSLog(@"ImageOrientation UP/DOWN");
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
NSLog(@"ImageOrientation LEFT");
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
NSLog(@"ImageOrientation RIGHT");
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
NSLog(@"ImageOrientation UP/DOWN");
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
// CGContextSetInterpolationQuality(bitmap, 1);
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}
请帮帮我
【问题讨论】:
-
因此,您无需将其调整为 75x75 的小尺寸,您只需更改内容模式即可将其显示在一个小的 imageView 中。对于单个图像来说,这不是一个坏主意。
-
@robin: 如果我更新图像并留下内存警告,应用程序会在设备上崩溃
标签: iphone