Png怎披,Jpg胸嘁,Gif

1.jpg-->png

- (void)jpgToPng{
    UIImage * image = [UIImage imageNamed:@"faust_github.jpg"];
    NSData * data = UIImagePNGRepresentation(image);
    UIImage * imagePng = [UIImage imageWithData:data];
    //圖片保存到本地相冊(cè)中
    UIImageWriteToSavedPhotosAlbum(imagePng, nil, nil, nil);
}

2.jpg-->jpg

- (void)jpgToJpg{
    UIImage * image = [UIImage imageNamed:@"faust_github.jpg"];
    NSData * data = UIImageJPEGRepresentation(image, 1);//1是質(zhì)量因子,越小凉逛,圖片越小
    UIImage * imagePng = [UIImage imageWithData:data];
    //圖片保存到本地相冊(cè)中
    UIImageWriteToSavedPhotosAlbum(imagePng, nil, nil, nil);
}

注:

  • 1.imageNamed imageWithData
  • 2.png無(wú)損性宏,,jpg有損

3.播放gif圖片

/**
 *  1.我們要拿到gif數(shù)據(jù)
 *  2.將gif分解一幀幀
 *  3.將單幀數(shù)據(jù)轉(zhuǎn)為UIImage
 *  4.單幀圖片保存
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self deCompositionGif];
    [self ShowGIf];
}
/**
 *  展示gif
 */
- (void)ShowGIf{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 270, 115)];
    [self.view addSubview:imageView];
    [imageView setAnimationImages:gifArray];
    [imageView setAnimationRepeatCount:10];
    [imageView setAnimationDuration:1];
    [imageView startAnimating];
}
/**
 *  分解gif
 */
- (void)deCompositionGif{
    //1.我們要拿到gif數(shù)據(jù)
    NSString * gifPathSource = [[NSBundle mainBundle] pathForResource:@"paopao" ofType:@"gif"];
    NSData * data = [NSData dataWithContentsOfFile:gifPathSource];
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //2.將gif分解一幀幀
    size_t count = CGImageSourceGetCount(source);
    NSMutableArray * tmpArray = [NSMutableArray arrayWithCapacity:0];
    for (size_t i = 0; i < count; i++) {
        CGImageRef imageref = CGImageSourceCreateImageAtIndex(source, i, NULL);
        //3.將單幀數(shù)據(jù)轉(zhuǎn)為UIImage
        UIImage * image = [UIImage imageWithCGImage:imageref scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tmpArray addObject:image];
        CGImageRelease(imageref);
    }
    CFRelease(source);
    //4.單幀圖片保存
    int i = 0;
    for (UIImage * img in tmpArray) {
        NSData * data = UIImagePNGRepresentation(img);
        NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * gifPath = path[0];
        NSLog(@"%@",gifPath);
        NSString * pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
        i++;
        [data writeToFile:pathNum atomically:YES];
    }
    gifArray = [NSMutableArray arrayWithArray:tmpArray];
}

4.png--->gif

- (void)createGif{
    //1.獲取我們的數(shù)據(jù)
    NSMutableArray * pngArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 1; i < 11; i++) {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",i]];
        [pngArray addObject:image];
    }
    //2.創(chuàng)建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);
    //3.配置gif屬性
    CGImageDestinationRef destion;
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    CGImageDestinationCreateWithURL(url, kUTTypeGIF, pngArray.count, NULL);
    NSDictionary * frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
    NSMutableDictionary * gifParaDict = [NSMutableDictionary dictionaryWithCapacity:0];
    [gifParaDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
    [gifParaDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    [gifParaDict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
    [gifParaDict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParaDict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    //4.單幀添加到gif
    for (UIImage * image in pngArray) {
        CGImageDestinationAddImage(destion, image.CGImage, (__bridge CFDictionaryRef) frameDic);
    }
    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destion);
    CFRelease(destion);
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末状飞,一起剝皮案震驚了整個(gè)濱河市毫胜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌诬辈,老刑警劉巖酵使,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異焙糟,居然都是意外死亡口渔,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門穿撮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)缺脉,“玉大人痪欲,你說(shuō)我怎么就攤上這事」ダ瘢” “怎么了业踢?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)礁扮。 經(jīng)常有香客問(wèn)我陨亡,道長(zhǎng),這世上最難降的妖魔是什么深员? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任负蠕,我火速辦了婚禮,結(jié)果婚禮上倦畅,老公的妹妹穿的比我還像新娘遮糖。我一直安慰自己,他們只是感情好叠赐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布欲账。 她就那樣靜靜地躺著,像睡著了一般芭概。 火紅的嫁衣襯著肌膚如雪赛不。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天罢洲,我揣著相機(jī)與錄音踢故,去河邊找鬼。 笑死惹苗,一個(gè)胖子當(dāng)著我的面吹牛殿较,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播桩蓉,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼淋纲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了院究?” 一聲冷哼從身側(cè)響起洽瞬,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎业汰,沒想到半個(gè)月后伙窃,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蔬胯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年对供,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡产场,死狀恐怖鹅髓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情京景,我是刑警寧澤窿冯,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站确徙,受9級(jí)特大地震影響醒串,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鄙皇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一芜赌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伴逸,春花似錦缠沈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至顷锰,卻和暖如春柬赐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背官紫。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工肛宋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人万矾。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓悼吱,卻偏偏與公主長(zhǎng)得像慎框,于是被迫代替她去往敵國(guó)和親良狈。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容