图片裁剪,图片体现中遇见的回降与裁剪难题
分类:计算机编程

// 裁剪图片  (UIImage *)imageWithImageSimple:( UIImage *)image scaledToSize:newSize{ UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0); [image drawInRect : CGRectMake ( 0 , 0 ,newSize. width ,newSize. height )]; UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return newImage;}

imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));

}

//对象方法

图片裁剪根据需求自行设置newSize如果是等比例裁剪(宽高比不变的情况下),可将从相册或者相机拿到的图片获取image.size,然后再将newSize按宽高比传入方法即可。

// 新形象从上下文

UIImage*newImg = [selfgetJPEGImagerImg:imageArr[i]compressibilityFactor:compressibilityFactor];

//根据网址将图片转化成image

return [UIImage imageWithCGImage:imageRef];

}

newSize.height = image.size.height;

}

NSData*dJpeg =nil;

//裁剪图片

// 创建一个图形上下文形象

oldImg_HEI = (compressibilityFactor* oldImg_HEI)/oldImg_WID;

imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));

} else {

CGFloat oldImg_HEI = image.size.height;

CGSize newSize;

}

if(oldImg_WID > compressibilityFactor || oldImg_HEI > compressibilityFactor){

{

imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));

- (NSArray*)getJPEGImagerImgArr:(NSArray*图片裁剪,图片体现中遇见的回降与裁剪难题。)imageArrcompressibilityFactor:(CGFloat)compressibilityFactor{

}

CGSize newSize;

CGFloat oldImg_WID = image.size.width;

//压缩图片

CGImageRef imageRef = nil;

dJpeg = UIImageJPEGRepresentation(newImg,0.5);

newSize.height = image.size.width * height /width;

//压缩图片

}

newSize.width = image.size.width;

{

return[UIImageimageWithData:dJpeg];

//图片剪切

//裁剪图片

}

if ((image.size.width / image.size.height) < (width / height)) {

return newImage;

NSMutableArray*newImgArr = [NSMutableArraynew];

UIImage * newImage = [image getImageFromUrl:imgUrl imgViewWidth:width imgViewHeight:height];

方法二:按照窗口宽高比例,将原图横向或者纵向裁剪掉多余的部分,然后不设置UIImageView的contentMode属性,将裁剪后的图片送进去,使其自动适应窗口。

oldImg_WID = KCompressibilityFactor;

UIImage * newImage = [self cutImage:image imgViewWidth:width imgViewHeight:height];

这时你可能想到设置UIImageView的属性 _imageView.contentMode = UIViewContentModeCenter;  设置该属性后,我们会发现图片的宽高比例确实正确了,但是窗口中只显示了图片的一部分,这说明图片整体尺寸没有压缩,按照原图进行显示了。怎样才能达到既缩小图片又不改变原来的宽高比例呢?

UIImage*newImg = [selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];

UIImage * image =[[UIImage alloc] init];

}

}

newSize.width = image.size.height * width / height;

newSize.width = image.size.height * _headerView.bgImgView.size.width / _headerView.bgImgView.size.height;

- (UIImage*)getJPEGImagerImg:(UIImage*)imagecompressibilityFactor:(CGFloat)compressibilityFactor{

//data 转image

UIGraphicsEndImageContext();

//超过设置的最大宽度 先判断那个边最长

} else {

newSize.width = image.size.width;

oldImg_HEI = (KCompressibilityFactor* oldImg_HEI)/oldImg_WID;

//根据宽高剪切图片

newSize.height = image.size.width * _headerView.bgImgView.size.height / _headerView.bgImgView.size.width;

#pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法

image =[UIImage imageWithData:data];

if ((image.size.width / image.size.height) < (_headerView.bgImgView.size.width / _headerView.bgImgView.size.height)) {

#pragma mark - 根据宽高压缩图片

imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));

- (UIImage *)image:(UIImage*)image scaledToSize:(CGSize)newSize

//宽度大于高度

}

// 新的尺寸

return[UIImageimageWithData:dJpeg];

return newImage;

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

oldImg_HEI = compressibilityFactor;

- (UIImage *)cutImage:image imgViewWidth:width imgViewHeight:height

- (UIImage *)cutImage:(UIImage*)image

#pragma mark - 压缩一张图片 自定义最大宽高

(UIImage *)getImageFromUrl:imgUrl imgViewWidth:width imgViewHeight:height;

结果表明方法二效果更好。

//宽度大于高度

CGImageRef imageRef = nil;

newSize.height = image.size.height;

returnnewImgArr;

NSData * data = [NSData dataWithContentsOfURL:imgUrl];

//压缩图片

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

(UIImage *)getImageFromUrl:imgUrl imgViewWidth:width imgViewHeight:height{

方法一:将图片按照原来的宽高比例压缩到与窗口合适的大小,然后在设置了_imageView.contentMode = UIViewContentModeCenter;  这个属性的UIImageView中展示压缩后的图片。

- (UIImage*)getJPEGImagerImg:(UIImage*)image{

#pragma mark-------根据imgView的宽高获得图片的比例

UIGraphicsBeginImageContext(newSize);

[newImgArraddObject:newImg];

}

// 结束上下文

oldImg_HEI = KCompressibilityFactor;

-(UIImage *)getImageFromUrl:imgUrl imgViewWidth:width imgViewHeight:height{

我们假设要在截图中的举行图片展示区显示图片,由于原图片的宽高比例与图片显示窗口的宽高比例不一定相同,所以,直接将图片扔进去会改变图片的宽高比例,展示效果不好。

//CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比

return [UIImage imageWithCGImage:imageRef];

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

dJpeg = UIImageJPEGRepresentation(newImg,0.5);

return newImage;

// 返回新形象

for(inti =0; i

}

// 告诉旧图片画在这个新的环境,所需的

}

前两天做项目,由于图片的大小比例与给定imageView的大小比例不一致,导致图片变形影响美观;现做以下操作

{

#pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法

UIImage * image ;

上面方法的参数newSize是和图片显示窗口差不多大的,结果出现了原图清晰,但压缩后图片不清晰的情况。

}

//CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比

- (NSArray*)getJPEGImagerImgArr:(NSArray*)imageArr{

CGFloat oldImg_HEI = image.size.height;

}

if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){

@implementationWeChatJPEGImager

[newImgArraddObject:newImg];

NSData*dJpeg =nil;

}else{

}

UIImage*newImg = [selfgetJPEGImagerImg:imageArr[i]];

oldImg_WID = compressibilityFactor;

}else{

[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

UIGraphicsBeginImageContext(newSize);

NSMutableArray*newImgArr = [NSMutableArraynew];

if(UIImagePNGRepresentation(newImg)==nil) {

}

UIGraphicsEndImageContext();

dJpeg = UIImagePNGRepresentation(newImg);

}else{

for(inti =0; i

if(oldImg_WID > oldImg_HEI){

#define KCompressibilityFactor 1280.00

dJpeg = UIImagePNGRepresentation(newImg);

CGFloat oldImg_WID = image.size.width;

- (UIImage*)imageWithImage:(UIImage*)imagescaledToSize:(CGSize)newSize{

returnnewImgArr;

oldImg_WID = (KCompressibilityFactor* oldImg_WID)/oldImg_HEI;

//超过设置的最大宽度 先判断那个边最长

}else{

returnnewImage;

}

}

}

UIImage*newImg = [selfimageWithImage:imagescaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];

if(oldImg_WID > oldImg_HEI){

oldImg_WID = (compressibilityFactor* oldImg_WID)/oldImg_HEI;

#pragma mark - 压缩多张图片 自定义最大宽高

if(UIImagePNGRepresentation(newImg)==nil) {

本文由pc28.am发布于计算机编程,转载请注明出处:图片裁剪,图片体现中遇见的回降与裁剪难题

上一篇:胡桃app分界面设计及设计标准,科学统一企图格 下一篇:没有了
猜你喜欢
热门排行
精彩图文