GIF 그림 재생 및 생성

4398 단어
우선 헤드 파일을 가져와야 합니다
#import 
#import 

GIF 그림 재생
    NSString *gifpath = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:gifpath];
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    // gif 
    size_t count = CGImageSourceGetCount(source);
    //    NSLog(@"===%zu",count);
    NSMutableArray *tempArray = [NSMutableArray array];
    for (size_t i = 0; i < count; i++) {
        CGImageRef imageref = CGImageSourceCreateImageAtIndex(source, i, NULL);
        UIImage *image = [UIImage imageWithCGImage:imageref scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tempArray addObject:image];
        // 
        CGImageRelease(imageref);
    }
    CFRelease(source);
    
    // 
//        for (int i = 0; i < tempArray.count; i++) {
//            NSData *data = UIImagePNGRepresentation(tempArray[i]);
//    
//            NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//            NSString *gifpath = path[0];
//            NSString *pathNum = [gifpath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
//    
//            [data writeToFile:pathNum atomically:NO];
//        }
    UIImageView *imageview = [[UIImageView alloc] init];
    imageview.bounds = CGRectMake(0, 0, 180, 180);
    imageview.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
    [self.view addSubview:imageview];
    
    [imageview setAnimationImages:tempArray];
    [imageview setAnimationRepeatCount:3];
    [imageview setAnimationDuration:2];
    
    [imageview startAnimating];

GIF 이미지 생성
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < 24; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Documents%d",i]];// 
        [images addObject:image];
    }
    
    // gif 
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentstr = [document objectAtIndex: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);
    // gif 
    CGImageDestinationRef destion;
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, NULL);
    
    //    NSDictionary *frameDic = [NSDictionary dictionaryWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil] forKeys:(NSString *)kCGImagePropertyGIFDelayTime];
    
    NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.0f] forKey:(NSString *)kCGImagePropertyGIFDelayTime] forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    
    
    NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
    [gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
    [gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    [gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
    [gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    // GIF
    for (UIImage *dimage in images) {
        CGImageDestinationAddImage(destion, dimage.CGImage, (__bridge CFDictionaryRef)frameDic);
    }
    
    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destion);
    CFRelease(destion);

좋은 웹페이지 즐겨찾기