-
2021-08-17 17:41:51
我遇到这样一个问题。步骤如下
1. UIImage 压缩 UIImage 转NSData 大小为500百多K.压缩到100K以内。 返回NSData
这时的NSData 为90多K
2.NSData 转成UIImage 存储
3.用到时,把UIImage 转成NSData 进行使用,这时NSData 大约为150 - 200左右。
很是奇怪。不知道为什么。
暂时的解决办法是,压缩后的NSData直接存储,不转成UiImage 存储。
更多相关内容 -
iOS UIImage设置圆角
2019-12-20 10:36:13新建UIImage分类如下: @interface UIImage (RoundedRectImage) - (UIImage *)setRoundedCorners:(CGFloat)radius andSize:(CGSize)size; @end #import "UIImage+RoundedRectImage.h" @implementation UIImage ...新建UIImage分类如下:
@interface UIImage (RoundedRectImage) - (UIImage *)setRoundedCorners:(CGFloat)radius andSize:(CGSize)size; @end
#import "UIImage+RoundedRectImage.h" @implementation UIImage (RoundedRectImage) - (UIImage *)setRoundedCorners:(CGFloat)radius andSize:(CGSize)size{ CGRect rect = CGRectMake(0, 0, size.width, size.height); UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); CGContextRef ctx = UIGraphicsGetCurrentContext(); UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)]; CGContextAddPath(ctx,path.CGPath); CGContextClip(ctx); [self drawInRect:rect]; CGContextDrawPath(ctx, kCGPathFillStroke); UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
-
iOS uiimage部分截取图片
2014-05-19 15:27:24stretchableImageWithLeftCapWidth resizableImageWithCapInsets -
iOS UIImage渲染模式UIImageRenderingMode
2020-04-17 15:10:55在IOS7之后增加创建UITabBarItem的方法的同时新增了图像渲染模式枚举UIImageRenderingMode。 typedef NS_ENUM(NSInteger, UIImageRenderingMode) { UIImageRenderingModeAutomatic, // Use the default rendering...在IOS7之后增加创建UITabBarItem的方法的同时新增了图像渲染模式枚举 UIImageRenderingMode。
typedef NS_ENUM(NSInteger, UIImageRenderingMode) { UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information } API_AVAILABLE(ios(7.0));
在该模式的枚举值中,一共存在三个值:
- UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
- UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。
- UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
UIImageRenderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。其他情况可以看下面的图例
使用示例:
UIImage *image = [UIImage imageNamed:@"icon01.png"]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
-
iOS UIImage 图片裁剪 , 旋转裁剪 , 缩放裁剪 , 平移
2019-09-29 10:28:23这里是本人项目的里用的全部代码,可以直接拷贝使用: CameraCutView.h ``` #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN ...@property (nonatomic) UIImage * mTargetImage; @property (no...这里是本人项目的里用的全部代码,可以直接拷贝使用:
CameraCutView.h
```
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CameraCutView : UIView
@property (nonatomic) UIImage * mTargetImage;
@property (nonatomic) UIImage * mResultImage;
@property (nonatomic, assign) CGSize originalImageViewSize;- (void) showCoverViewWithTargetImg;
//frame 相对于当前屏幕坐标 当前屏幕当中的裁剪范围
-(UIImage *) cutImageWithSpecificRect:(CGRect)frame;
- (UIImage *)selectImage;// 旋转
- (void)rotationImage:(CGFloat)rotation ;// 缩放大小
- (void)setZoomWithValue:(CGFloat)zoom;
@endNS_ASSUME_NONNULL_END
```
CameraCutView.m
NS_ASSUME_NONNULL_END
#import "CameraCutView.h"
#import "UIImage+Util.h"@interface CameraCutView ()
@end
@implementation CameraCutView{
UIImageView *_mCameraBgView;
}- (instancetype)init{
if (self = [super init]) {
[self initUI];
return self;
}
return nil;
}- (void)initUI{
self.backgroundColor = [UIColor clearColor];
}- (void)showCoverViewWithTargetImg {
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
_mCameraBgView.backgroundColor = kColorHex(kColorWhite);
float scaleValue = [self getScaleNum:_mTargetImage];
_mTargetImage = [self scaleImage:_mTargetImage toScale:scaleValue];
if (_mTargetImage != nil) {
_mCameraBgView = [[UIImageView alloc] init];
}
float _imageScale = self.frame.size.width / _mTargetImage.size.width;
_mCameraBgView.frame = CGRectMake(0, 0, _mTargetImage.size.width * _imageScale, _mTargetImage.size.height * _imageScale);
_originalImageViewSize = CGSizeMake(_mTargetImage.size.width * _imageScale, _mTargetImage.size.height * _imageScale);
_mCameraBgView.image = self.mTargetImage;
[self addSubview:_mCameraBgView];
[self setUserGesture];
}- (void)setUserGesture {
[_mCameraBgView setUserInteractionEnabled:YES];
//添加移动手势
UIPanGestureRecognizer *moveGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[moveGes setMinimumNumberOfTouches:1];
[moveGes setMaximumNumberOfTouches:1];
[_mCameraBgView addGestureRecognizer:moveGes];
//添加缩放手势
UIPinchGestureRecognizer *scaleGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
[_mCameraBgView addGestureRecognizer:scaleGes];
//添加旋转手势
UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
[_mCameraBgView addGestureRecognizer:rotateGes];
}// 上下移动
float _lastTransX = 0.0, _lastTransY = 0.0;
- (void)moveImage:(UIPanGestureRecognizer *)sender {
CGPoint translatedPoint = [sender translationInView:self];
if([sender state] == UIGestureRecognizerStateBegan) {
_lastTransX = 0.0;
_lastTransY = 0.0;
}
CGAffineTransform trans = CGAffineTransformMakeTranslation(translatedPoint.x - _lastTransX, translatedPoint.y - _lastTransY);
CGAffineTransform newTransform = CGAffineTransformConcat(_mCameraBgView.transform, trans);
_lastTransX = translatedPoint.x;
_lastTransY = translatedPoint.y;
if ([self isCanMove:newTransform]) {
_mCameraBgView.transform = newTransform;
}
}// 旋转按钮事件
- (void)rotationImage:(CGFloat)rotation {
CGFloat newRotation = rotation;
NSLog(@"newRotation == %f", newRotation);
CGAffineTransform currentTransform = _mCameraBgView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, newRotation);
[_mCameraBgView setTransform:newTransform];
_lastRotation = rotation;
}// 放大、缩小按钮
- (void)setZoomWithValue:(CGFloat)zoom {
[self zoomImageWithScale:zoom];
}// 缩放手势
float _lastScale = 1.0;
- (void)scaleImage:(UIPinchGestureRecognizer *)sender {
if([sender state] == UIGestureRecognizerStateBegan) {
_lastScale = 1.0;
return;
}
CGFloat scale = [sender scale]/_lastScale;
[self zoomImageWithScale:scale];
_lastScale = [sender scale];
}- (void)zoomImageWithScale:(CGFloat)scale {
// scale 小于1为缩小
if (scale < 1) {
if (_mCameraBgView.size.width < kScreenWidth) {
return;
}
}
// scale 大于1为放大
if (scale > 1) {
if (_mCameraBgView.size.width > kScreenWidth * 3.5) {
return;
}
}
CGAffineTransform currentTransform = _mCameraBgView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[_mCameraBgView setTransform:newTransform];
}// 旋转手势
float _lastRotation = 0.0;
- (void)rotateImage:(UIRotationGestureRecognizer *)sender {
if ([sender state] == UIGestureRecognizerStateEnded) {
_lastRotation = 0.0;
return;
}
CGFloat rotation = -_lastRotation + [sender rotation];
CGAffineTransform currentTransform = _mCameraBgView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
[_mCameraBgView setTransform:newTransform];
_lastRotation = [sender rotation];
}/***
方法名称:cutImageWithSpecificRect
方法用途:根据特定的区域对图片进行裁剪
方法说明:核心裁剪方法CGImageCreateWithImageInRect(CGImageRef image,CGRect rect)
***/
- (UIImage *)cutImageWithSpecificRect:(CGRect)frame {
float zoomScale = [[_mCameraBgView.layer valueForKeyPath:@"transform.scale.x"] floatValue];
float rotate = [[_mCameraBgView.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
float _imageScale = _mTargetImage.size.width/_originalImageViewSize.width;
//裁剪区域的Size_originalImageViewSize CGSize (width = 0, height = 0)
CGSize cropSize = CGSizeMake(frame.size.width/zoomScale, frame.size.height/zoomScale);
//裁剪区域的Origin
CGPoint cropperViewOrigin = CGPointMake((0.0 - _mCameraBgView.frame.origin.x + frame.origin.x)/zoomScale,
(0.0 - _mCameraBgView.frame.origin.y + frame.origin.y)/zoomScale);
if((NSInteger)cropSize.width % 2 == 1)
{
cropSize.width = ceil(cropSize.width);
}
if((NSInteger)cropSize.height % 2 == 1)
{
cropSize.height = ceil(cropSize.height);
}
CGRect CropRectinImage = CGRectMake((NSInteger)(cropperViewOrigin.x*_imageScale) ,(NSInteger)( cropperViewOrigin.y*_imageScale), (NSInteger)(cropSize.width*_imageScale),(NSInteger)(cropSize.height*_imageScale));
UIImage *rotInputImage = [_mTargetImage imageRotatedByRadians:rotate];
CGImageRef tmp = CGImageCreateWithImageInRect([rotInputImage CGImage], CropRectinImage);
self.mResultImage = [UIImage imageWithCGImage:tmp scale:_mTargetImage.scale orientation:_mTargetImage.imageOrientation];
CGImageRelease(tmp);
return self.mResultImage;
}- (UIImage *)selectImage {
//self.image是拍照所得的照片
UIImage *image1 = _mTargetImage;
CGImageRef cgRef = image1.CGImage;
//实际照片大小与屏幕大小之比
CGFloat widthScale = image1.size.width / kScreenWidth;
CGFloat heightScale = image1.size.height / kScreenHeight;
//我们所拍照片其实是横屏的
//多减掉50是因为最后的效果图片的高度有偏差,不知道原因
CGFloat orignWidth = 226-50;//226
CGFloat orginHeight = 360;//360CGFloat x = (kScreenHeight - orginHeight) * 0.5 * heightScale;
CGFloat y = (kScreenWidth - orignWidth) * 0.5 * widthScale;
CGFloat width = orginHeight * heightScale;
CGFloat height = orignWidth * widthScale;
CGRect r = CGRectMake(x, y, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect(cgRef, r);
UIImage *thumbScale = [UIImage imageWithCGImage:imageRef];
//
image1 = thumbScale;
return image1;
}- (BOOL)isCanMove:(CGAffineTransform )newTransform {
if (_mCameraBgView.frame.size.height/2 - fabs(newTransform.ty)<=0 ||
_mCameraBgView.frame.size.width/2 - fabs(newTransform.tx) <=0) {
return NO;
} else {
return YES;
}
}/***
方法名称:scaleImage:toScale:
方法用途:图片的伸缩处理
方法说明:scaleSize:放大或缩小的倍数
***/
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize {
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
/***
方法名称:getScaleNum:
方法用途:根据图片的宽度为基准,来获取图片伸缩放大的倍数
方法说明:
***/
- (float)getScaleNum:(UIImage *)targetImg {
CGRect r = [UIScreen mainScreen].bounds;
float preWidth = targetImg.size.width;
float scaleValue = 1;
// scaleValue = r.size.width/preWidth;
return scaleValue;
}//图片复原
- (void)reset {
_mCameraBgView.transform = CGAffineTransformIdentity;
}@end
UIImage+Util.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (Util)
//设置的外围不变形内部平铺拉伸
- (UIImage*)resizeImageWithTop:(CGFloat)top andLeft:(CGFloat)left andBottom:(CGFloat)bottom andRight:(CGFloat)right;
- (UIImage *)imageRotatedByRadians:(CGFloat)radians;
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;@end
NS_ASSUME_NONNULL_END
UIImage+Util.m
#import "UIImage+Util.h"
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};@implementation UIImage (Util)
//设置的外围不变形内部平铺拉伸
- (UIImage*)resizeImageWithTop:(CGFloat)top andLeft:(CGFloat)left andBottom:(CGFloat)bottom andRight:(CGFloat)right{
UIImage *image = self;
// 设置端盖的值
CGFloat _top = image.size.height * top;
CGFloat _left = image.size.width * left;
CGFloat _bottom = image.size.height * bottom;
CGFloat _right = image.size.width * right;
// 设置端盖的值
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(_top, _left, _bottom, _right);
// 设置拉伸的模式
UIImageResizingMode mode = UIImageResizingModeStretch;
// 拉伸图片
UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
return newImage;
}- (UIImage *)imageRotatedByRadians:(CGFloat)radians {
return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
}- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// // Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}@end
下面是调用代码
@interface BitiPhotoVC ()
@property (nonatomic, strong) CameraCutView *cutView;@end
// 图片展示
self.cutView.mTargetImage = [UIImage imageNamed:<#(nonnull NSString *)#>]
self.cutView.originalImageViewSize = CGSizeMake(width, height);
[self.cutView showCoverViewWithTargetImg]; -
iOS中 UIImage根据屏宽调整size的实例代码
2021-01-05 05:27:42需求:UIImage根据屏幕宽度按照自己本身比例改变高度 上代码,为UIImage创建一个Category #import UIImage+UIImageExtras.h @implementation UIImage (UIImageExtras) - (UIImage *)imageByScalingToSize:(CGSize)... -
iOS UIImage等比缩放、转到横屏的图片到垂直模式、重制大小resize
2021-04-14 17:13:14UIImage等比缩放 分类方法 @implementation UIImage (Cat) // 计算等比缩放图片的size - (CGSize)equalRatioComputeImageWithTargetWidth:(CGFloat)targetWidth targetHeight:(CGFloat)targetHeight { CGFloat ... -
ios UIImage转RGB
2018-12-25 21:02:05有时需要一些特殊目的, ...+ (unsigned char *)rgbArray: (UIImage *) uiimage { CGImageRef image = [uiimage CGImage]; CGSize size = uiimage.size; CGColorSpaceRef colorSpace = CGColorSpaceCreateDe... -
iOS UIImage 转NSString
2015-08-12 13:16:25项目中可能会有这样的需求,比如图片上传服务器时一般都会转成NSString再上传 那么问题来了,图片如何转为NSString ,已经被...-(NSString *)UIImageToBase64Str:(UIImage *) image { NSData *data = UIImageJPEGR -
iOS UIImage加载图片的问题
2015-07-28 22:28:11[UIImage imageNamed:@"icon.png"] 用上面的方法加载图片有问题。该方法即可以从bundle中读取图片。 这种方法在application bundle的顶层文件夹寻找由供应的名字的图象 。 如果找到图片,装载到iPhone系统... -
iOS UIImage方法总结
2014-11-13 21:01:49IOS UIImage类方法总结 IOS中对图片的处理 UIImage 相信做项目时肯定会有用到 UIImage 这个类,那我们就来看一下这个类中都有什么内容。 其实这篇文章就是在看文档的时候想记录一下文档中得方法。 ... -
iOS UIImage图片与base64编码相互转换
2017-04-19 10:37:00UIImage *img = [UIImage imageNamed:@"test.png"]; NSData *data = UIImageJPEGRepresentation(img, 1.0f); NSString * ImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64... -
iOS UIimage拉伸的三种方法
2016-04-13 09:48:49//用到image的这个方法- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight //leftCapWidth:左边不拉伸区域 //topCapHeight:上面不拉伸区域 ... -
iOS UIImage的解码时机
2017-09-26 17:49:08UIImage的图片转码时机是在GPU绘制图像时 -
iOS UIImage类扩展(按照位置和大小截图图片中部分图片)
2016-05-27 11:43:12声明import -
iOS UIImage 装换成 base64字符串
2015-04-16 15:38:251. 我不知道你为什么不找你的... 不管怎么说, 您需要先添加NSData类别到您的项目,该项目可以从这里- 头和 然后将您的UIImage成对象NSData方式如下: NSData *imageData = UIImageJPEGRepresentation(image, 1.0); -
iOS 调整UIImage图片大小
2019-07-21 22:37:26/** * 重设图片大小 */ func reSizeImage(reSize : CGSize) -> UIImage { //UIGraphicsBeginImageContext(reSize); UIGraphicsBeginImageContextWithOptions(reSize, false, UIScreen.m... -
iOS UIImage类 imageNamed方法使用不当
2017-06-06 17:25:53今天碰到这么一个问题,push到一个新页面,该页面用for循环UIImage的imageNamed 去批量加载图片,赋值给imageview,执行animationImages动画。第一次push发现存在延时,第二次后就不出现延时状况。经查找,发现是由... -
ios UIImage修改大小后图片模糊解决办法
2015-06-03 15:19:45这个方法不100%保证能解决,但是大...- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); [img drawInRect:CGRect -
iOS UIImage渲染模式 imageWithRenderingMode:
2016-07-04 15:51:12着色(Tint Color)是iOS7界面中的一个设置UIImage的渲染模式,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。UIImage新增了一个只读属性:renderingMode,对应的还有一个新增方法:... -
iOS将UIImage图片切成圆形
2022-01-20 22:44:51在开发过程中我们要尽量避免离屏渲染,虽说现在对 UIImageView.layer 进行圆角设置和遮罩不会触发...我们创建一个UIImage的分类 UIImage+Circle,声明一个方法 circle 并实现 //.h #import <UIKit/UIKit.h> N -
ios UIImage category 图片加载
2012-12-23 12:48:48尽量避免使用imageNamed,如果图像比较大,或者图像比较多,用这种方式会消耗很大的内存。 -
【iOS开发】CMSampleBuffer 和 UIImage 相互转换
2022-02-22 17:56:31iOS CMSampleBuffer 转换 UIImage CMSampleBuffer 转换 UIImage 第一种方法: /// Convert CMSampleBuffer to UIImage func WM_FUNC_sampleBufferToImage(_ sampleBuffer:CMSampleBuffer) -> UIImage { let ... -
iOS UIImage根据屏宽调整size
2017-01-18 10:33:29需求:UIImage根据屏幕宽度按照自己本身比例改变高度 上代码,为UIImage创建一个Category #import "UIImage+UIImageExtras.h" @implementation UIImage (UIImageExtras) - (UIImage *)... -
iOS UIImage 图像旋转
2016-02-25 10:54:14iOS UIImage 图像旋转 vImg:待旋转的图 vAngle:旋转角度 vIsExpand:是否扩展,如果不扩展,那么图像大小不变,但被截掉一部分 */ - (UIImage*)rotateImageWithAngle:(UIImage*)vImg Angle:(CGFloat)vAngle ... -
iOS UIImage 图片裁剪 , 旋转裁剪 , 缩放裁剪 , 平移裁剪 有demo下载
2018-07-03 16:31:07UIImage * mTargetImage; @property ( nonatomic ) UIImage * mResultImage; @property ( nonatomic , assign ) CGFloat ImgCutHeight; @property ( nonatomic , assign ) CGSize ... -
iOS 剪切,压缩图片UIImage的几种方法
2021-11-01 17:02:54+(UIImage *)compressImageSize:(UIImage *)image toByte:(NSUInteger)maxLength{ CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(image, compression); if (data.length < maxLength) ... -
iOS应用开发中对UIImage进行截取和缩放的方法详解
2021-01-05 15:40:14截取UIImage指定大小区域 最近遇到这样的需求:从服务器获取到一张照片,只需要显示他的左半部分,或者中间部分等等。也就是截取UIImage指定大小区域。 UIImage扩展: 我的解决方案是对UIImage进行扩展。通过... -
iOS 修改UIImage大小
2016-07-05 16:48:47在iOS中,uiimage没有用于修改大小的属性,要在代码中改变uiimage图片的大小,需要扩展UIImage类,如下: 头文件: #import @interface UIImage (UIImageExtras) - (UIImage *)...