本文介绍了iPhone:创建视频缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用UIImagePickerController didFinishPickingMediaWithInfo提供的信息创建缩略图?
How can I create a thumbnail image from info provided by UIImagePickerController didFinishPickingMediaWithInfo?
推荐答案
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;
}
只要传递要创建缩略图的大小即可。
just pass size of which you want to create thumb image.
这篇关于iPhone:创建视频缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!