iOS基础之UIImageView(一)

1 图存保存到相册

UIImage *image = [UIImage imageNamed:@"test.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

2 jpg 转 png

- (void)JPGToPNG {
    UIImage *image = [UIImage imageNamed:@"test.jpg"];
    NSData *data = UIImagePNGRepresentation(image);
    UIImage *imagePNG = [UIImage imageWithData:data];
    UIImageWriteToSavedPhotosAlbum(imagePNG, nil, nil, nil);
}

3 jpg 转 jpg

- (void)JPGToJPG {
    UIImage *image = [UIImage imageNamed:@"test.jpg"];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    UIImage *imageJPG = [UIImage imageWithData:data];
    UIImageWriteToSavedPhotosAlbum(imageJPG, nil, nil, nil);
}

4 gif 图片分解

/**
 gif 图片分解
 1 拿到 gif 数据
 2 将 gif 图片分解为一帧帧的
 3 将单帧数据转化为UIImage
 4 单帧图片保存
 */
- (void)decomposeGIF {
    // 1 拿到 gif 数据
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    // 2 将 gif 图片分解为一帧帧的
    size_t count = CGImageSourceGetCount(source);
    NSMutableArray *tempArray = [NSMutableArray array];
    for(size_t i = 0; i < count; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        // 3 将单帧数据转化为UIImage
        UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tempArray addObject:image];
        CGImageRelease(imageRef);
    }
    CFRelease(source);
    // 4 单帧图片保存
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *gifPath = path[0];
    int i = 0;
    for (UIImage *image in tempArray) {
        NSData *data = UIImagePNGRepresentation(image);
        NSString *pathNum = [gifPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", i]];
        i++;
        [data writeToFile:pathNum atomically:NO];
    }
}

5  gif 动画展示

/**
 gif 动画展示
 */
- (void)showGIFAnimation {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 750 * 0.5, 810 * 0.5)];
    [self.view addSubview:imageView];
    
    NSMutableArray *tempArray = [NSMutableArray array];
    for(int i = 0; i < 48; i++) {
        [tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];
    }
    [imageView setAnimationImages:tempArray];
    [imageView setAnimationDuration:2];
    [imageView setAnimationRepeatCount:10];
    [imageView startAnimating];
}

 6 gif 图片合成

/**
 gif 图片合成
 1 获取图片数据
 2 创建 gif 文件
 3 配置 gif 属性
 4 单帧图片添加到 gif
 */
- (void)createGIF {
    // 1 获取图片数据
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < 48; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d.png", i];
        [images addObject:[UIImage imageNamed:imageName]];
    }
    // 2 创建 gif 文件
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentStr = document[0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *textDic = [documentStr stringByAppendingString:@"/gif"];
    [fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDic stringByAppendingString:@"test1.gif"];
    NSLog(@"%@", path);
    // 3 配置 gif 属性
    CGImageDestinationRef destination;
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, NULL);
    NSDictionary *frameDic = @{(NSString *)kCGImagePropertyGIFDelayTime: @{(NSString *)kCGImagePropertyGIFDelayTime: @(0.3)}};
    NSDictionary *gifParmDict = @{(NSString *)kCGImagePropertyGIFHasGlobalColorMap : @(YES),
                                         (NSString *)kCGImagePropertyColorModel: (NSString *)kCGImagePropertyColorModelRGB,
                                         (NSString *)kCGImagePropertyDepth: @(8),
                                         (NSString *)kCGImagePropertyGIFLoopCount: @(0)};
    NSDictionary *gifProperty = @{(NSString *)kCGImagePropertyGIFDictionary: gifParmDict};
    // 4 单帧图片添加到 gif
    for (UIImage *image in images) {
        CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameDic);
    }
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
}

 

posted @ 2018-07-13 15:02  紫洁  阅读(368)  评论(0编辑  收藏  举报