#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, XYCropImageStyle){
XYCropImageStyleRight =0, // 右半部分
XYCropImageStyleCenter =1, // 中间部分
XYCropImageStyleLeft =2, // 左半部分
XYCropImageStyleRightOneOfThird =3, // 右侧三分之一部分
XYCropImageStyleCenterOneOfThird =4, // 中间三分之一部分
XYCropImageStyleLeftOneOfThird =5, // 左侧三分之一部分
XYCropImageStyleRightQuarter =6, // 右侧四分之一部分
XYCropImageStyleCenterRightQuarter =7, // 中间右侧四分之一部分
XYCropImageStyleCenterLeftQuarter =8, // 中间左侧四分之一部分
XYCropImageStyleLeftQuarter =9, // 左侧四分之一部分
};
@interface UIImage (Crop)
- (UIImage *)imageByCroppingWithStyle:(XYCropImageStyle)style;
@end
UIImage+Crop.m
#import "UIImage+Crop.h"
@implementation UIImage (Crop)
- (UIImage *)imageByCroppingWithStyle:(XYCropImageStyle)style
{
CGRect rect;
switch (style) {
case XYCropImageStyleLeft:
rect = CGRectMake(0, 0, self.size.width/2, self.size.height);
break;
case XYCropImageStyleCenter:
rect = CGRectMake(self.size.width/4, 0, self.size.width/2, self.size.height);
break;
case XYCropImageStyleRight:
rect = CGRectMake(self.size.width/2, 0, self.size.width/2, self.size.height);
break;
case XYCropImageStyleLeftOneOfThird:
rect = CGRectMake(0, 0, self.size.width/3, self.size.height);
break;
case XYCropImageStyleCenterOneOfThird:
rect = CGRectMake(self.size.width/3, 0, self.size.width/3, self.size.height);
break;
case XYCropImageStyleRightOneOfThird:
rect = CGRectMake(self.size.width/3*2, 0, self.size.width/3, self.size.height);
break;
case XYCropImageStyleLeftQuarter:
rect = CGRectMake(0, 0, self.size.width/4, self.size.height);
break;
case XYCropImageStyleCenterLeftQuarter:
rect = CGRectMake(self.size.width/4, 0, self.size.width/4, self.size.height);
break;
case XYCropImageStyleCenterRightQuarter:
rect = CGRectMake(self.size.width/4*2, 0, self.size.width/4, self.size.height);
break;
case XYCropImageStyleRightQuarter:
rect = CGRectMake(self.size.width/4*3, 0, self.size.width/4, self.size.height);
break;
default:
break;
}
CGImageRef imageRef = self.CGImage;
CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
UIImage *cropImage = [UIImage imageWithCGImage:imagePartRef];
CGImageRelease(imagePartRef);
return cropImage;
}
UIImageView *imgView = [[UIImageView alloc] init]; imgView.frame = CGRectMake((SCREEN.width - 226) / 2, 100, 226, 106); UIImage *image = [UIImage imageNamed:@"ganggang"]; imgView.image = image; [self.view addSubview:imgView];
#import "UIImage+Crop.h"
image = [image imageByCroppingWithStyle:XYCropImageStyleLeft];
- (UIImage*)imageCompressWithSimple:(UIImage*)image scaledToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0,size.width,size.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage*)imageCompressWithSimple:(UIImage*)image scale:(float)scale
{
CGSize size = image.size;
CGFloat width = size.width;
CGFloat height = size.height;
CGFloat scaledWidth = width * scale;
CGFloat scaledHeight = height * scale;
UIGraphicsBeginImageContext(size); // this will crop
[image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
UIImage* newImage= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = [UIImage imageNamed:@"test.jpg"];
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width= scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有