iOS UIImageView
2019-01-02 本文已影响4人
CaptainRoy
- jpg 格式转化 png
-(UIImage *)transformPNGWithJPG:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
NSData *data = UIImagePNGRepresentation(image);
UIImage *pngImage = [UIImage imageWithData:data];
return pngImage;
}
- png 格式转化 jpg
-(UIImage *)transformJPGWithPNG:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
NSData *data = UIImageJPEGRepresentation(image, 1);
UIImage *jpgImage = [UIImage imageWithData:data];
return jpgImage;
}
将GIF分解为一帧帧 并保存到沙盒中
-(void)decomposeGIF
{
// 获取GIF数据
NSString *gifPathSource = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:gifPathSource];
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
// gif 每一帧分解
size_t count = CGImageSourceGetCount(sourceRef);
NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init];
for (size_t i = 0; i < count; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, i, NULL);
// 将单帧数据转化为 UIImage
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[tempMutableArray addObject:image];
CGImageRelease(imageRef);
}
CFRelease(sourceRef);
// 将图片保存到 Library/Caches/gifs目录下
NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; // Library/Caches
NSString *gifsPath = [NSString stringWithFormat:@"%@/gifs",cacheDirectory];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:gifsPath]) {
[fileManager createDirectoryAtPath:gifsPath withIntermediateDirectories:YES attributes:NULL error:NULL];
}
for (NSInteger i = 0; i < tempMutableArray.count; i++) {
UIImage *image = tempMutableArray[i];
NSData *data = UIImagePNGRepresentation(image);
NSString *imageName = [NSString stringWithFormat:@"%@/%ld.png",gifsPath,(long)i];
[data writeToFile:imageName atomically:NO];
}
}
编辑图片- 旋转图片然后保存
- UIImage 分类的实现方法
#import "UIImage+Rotate.h"
#import <QuartzCore/QuartzCore.h>
#import <Accelerate/Accelerate.h>
@implementation UIImage (Rotate)
-(UIImage *)imageRotateIndegree:(CGFloat)degree
{
size_t width = (size_t)self.size.width * self.scale;
size_t height = (size_t)self.size.height * self.scale;
CGFloat bytesPerRow = width * 4; // 每行 图片数据字节
CGImageAlphaInfo alphaInfo = kCGImageAlphaPremultipliedFirst;
// 配置上下文参数
CGContextRef contextRef = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGBitmapByteOrderDefault | alphaInfo);
if (!contextRef) {
return nil;
}
CGContextDrawImage(contextRef, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), self.CGImage);
// 旋转
UInt8 *data = (UInt8 *)CGBitmapContextGetData(contextRef);
vImage_Buffer src = {data,height,width,bytesPerRow};
vImage_Buffer dest = {data,height,width,bytesPerRow};
Pixel_8888 bgColor = {0,0,0,0};
vImageRotate_ARGB8888(&src, &dest, NULL, degree, bgColor, kvImageBackgroundColorFill);
CGImageRef rotateImageRef = CGBitmapContextCreateImage(contextRef);
UIImage *rotateImage = [UIImage imageWithCGImage:rotateImageRef scale:self.scale orientation:UIImageOrientationUp];
return rotateImage;
}
@end
- 实现
UIImage *image = [UIImage imageNamed:@"christmas.jpg"];
NSData *data = UIImagePNGRepresentation(image);
UIImage *pngImage = [UIImage imageWithData:data];
UIImage *rotateImage = [pngImage imageRotateIndegree:M_PI / 180 * 45];
UIImageWriteToSavedPhotosAlbum(rotateImage, NULL, NULL, NULL);
图片剪切
-(UIImage *)imageCutSize:(CGRect)rect
{
CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
CGRect smallRect = CGRectMake(0.0f, 0.0f, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
UIGraphicsBeginImageContext(smallRect.size);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextDrawImage(contextRef, smallRect, subImageRef);
UIImage *image = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return image;
}
*实现
UIImage *cutImage = [pngImage imageCutSize:CGRectMake(100.0f, 100.0f, 20.0f, 20.0f)];
UIImageWriteToSavedPhotosAlbum(cutImage, NULL, NULL, NULL);