如何根据UIImagePickerController didFinishPickingMediaWithInfo提供的信息创建缩略图?
答案 0 :(得分:1)
static inline double radians (double degrees) {
return degrees * M_PI/180;
}
- (UIImage*) imageByScalingToSize:(CGSize)targetSize image:(UIImage*)image {
UIImage* sourceImage = image;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat sourceHeight = image.size.height;
CGFloat sourceWidth = image.size.width;
if(sourceHeight < sourceWidth) {
CGFloat ratio = sourceWidth/sourceHeight;
targetWidth = ratio*targetHeight;
}else if(sourceHeight > sourceWidth) {
CGFloat ratio = sourceHeight/sourceWidth;
targetHeight = ratio*targetWidth;
}
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if(sourceImage.imageOrientation == UIImageOrientationRight) {
float temp = targetWidth;
targetWidth = targetHeight;
targetHeight = temp;
}
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 *newImage1 = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage1;
}
只传递你想要创建拇指图像的大小。