//获取图片的缩略图
+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height) {
rect.size.width = asize.height*oldsize.width/oldsize.height;
rect.size.height = asize.height;
rect.origin.x = (asize.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = asize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
//获取等比例压缩的图像
+ (UIImage *)getPicZoomImage:(UIImage *)image {
UIImage *img = image;
int h = img.size.height;
int w = img.size.width;
if(h <= PicAfterZoomWidth && w <= PicAfterZoomHeight)
{
image = img;
}
else
{
float b = (float)PicAfterZoomWidth/w < (float)PicAfterZoomHeight/h ? (float)PicAfterZoomWidth/w : (float)PicAfterZoomHeight/h;
CGSize itemSize = CGSizeMake(b*w, b*h);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0, 0, b*w, b*h);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return img;
}
/**
* 修改图片大小
*/
+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{
newSize.height=image.size.height*(newSize.width/image.size.width);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//截取图片 根据frame
- (NSArray *)captureViews:(UIView *)theView frame:(NSArray *)fra{
@autoreleasepool {
NSMutableArray *imageViewArrReturn = [NSMutableArray array];
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
theView.layer.contents =nil;
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
for (int i = 0; i < fra.count; i++) {
NSValue *frame = [fra objectAtIndex:i];
CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, [frame CGRectValue]);
UIImage *image = [UIImage imageWithCGImage:ref];
[imageViewArrReturn addObject:image];
CGImageRelease(ref);
}
CGContextRelease(context);
return imageViewArrReturn;
}
}
UIGraphicsBeginImageContext UIGraphicsGetImageFromCurrentImageContext 压缩,修改,截取图片
最新推荐文章于 2024-03-04 08:58:33 发布