//
|
// UIImage+YTH.m
|
// PapayaPlayerDaqo
|
//
|
// Created by 味口 on 15-6-3.
|
// Copyright (c) 2015年 wgj. All rights reserved.
|
//
|
|
#import "UIImage+YTH.h"
|
|
@implementation UIImage (YTH)
|
|
|
+ (UIImage *)resizedImageWithName:(NSString *)imageName
|
{
|
return [self resizedImageWithName:imageName left:0.5 top:0.5];
|
}
|
|
+ (UIImage *)resizedImageWithName:(NSString *)imageName left:(CGFloat)left top:(CGFloat)top
|
{
|
UIImage *image = [self imageNamed:imageName];
|
return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.height * top];
|
}
|
|
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
|
{
|
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
|
UIGraphicsBeginImageContext(rect.size);
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
CGContextSetFillColorWithColor(context, [color CGColor]);
|
CGContextFillRect(context, rect);
|
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
UIGraphicsEndImageContext();
|
return image;
|
}
|
|
//对图片尺寸进行压缩--
|
|
-(UIImage*)imageScaledToSize:(CGSize)newSize
|
{
|
// Create a graphics image context
|
UIGraphicsBeginImageContext(newSize);
|
|
// Tell the old image to draw in this new context, with the desired
|
// new size
|
[self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
|
|
// Get the new image from the context
|
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
// End the context
|
UIGraphicsEndImageContext();
|
|
return newImage;
|
}
|
|
- (UIImage *)imageByScalingToSize:(CGSize)targetSize{
|
// 创建一个bitmap的context
|
UIGraphicsBeginImageContext(targetSize);
|
// 绘制改变大小的图片
|
[self drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
|
// 从当前context中创建一个改变大小后的图片
|
UIImage *TransformedImg=UIGraphicsGetImageFromCurrentImageContext();
|
// 使当前的context出堆栈
|
UIGraphicsEndImageContext();
|
// 返回新的改变大小后的图片
|
return TransformedImg;
|
}
|
|
@end
|