2015-10-08 37 views
3

我指的是post。我試圖用截圖創建的圖像製作一個gif文件。我使用計時器來創建屏幕快照,以便獲取可用於創建gif的所需幀數。我每隔0.1秒採取一次快拍(我會在3秒後結束這個定時器)。用UIImages創建一個gif

這裏是我的代碼,以我的UIView的快照:

-(void)recordScreen{ 

    self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES]; 

} 

-(void)takeSnapShot{ 

    //capture the screenshot of the uiimageview and save it in camera roll 
    UIGraphicsBeginImageContext(self.drawView.frame.size); 
    [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

} 

,我指的是後,顯示一個輔助函數來創建GIF。我不知道我應該如何將我的圖像傳遞給幫助函數。這裏是我的嘗試:

我試圖修改此部分:

static NSUInteger kFrameCount = 10; 
for (NSUInteger i = 0; i < kFrameCount; i++) { 
     @autoreleasepool { 
      UIImage *image = [self takeSnaphot]; 
      CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); 
     } 
    } 

這樣就創建了我的UIView的10幀的GIF。

現在....

我所試圖做的是:

我對我的使用UIBeizerPath和我正在UIView快照畫一個簡單的繪圖用手指在平行於我的畫,所以我將有大約50-100個PNG文件。我試圖將所有這些圖像傳遞給makeGifMethod。

工作流程:

  1. 啓動應用程序。上的按鈕
  2. 輕按該啓動定時器,並採取圖片爲每0.1秒
  3. 平局與手指(所以所有附圖將被捕獲埃夫0.1秒)
  4. 每個快照後,我請makeanimatedgif的方法,所以將要加入到GIF前一幀拍攝的快照文件
  5. 停止一切

問題:

- 情況1:

  1. 按鈕點擊(它創建GIF immediatley)
  2. 開始繪製
  3. 停止
  4. 檢查GIF(具有10幀的GIF與白色背景創建的,因爲當我按下按鈕時,我什麼也沒抽出)

如果我在循環中調用snapShot方法,我會得到我繪圖的最後10幀,但不是所有東西。

for (NSUInteger i = 0; i < 10; i++) { 
     @autoreleasepool { 
      UIImage *image = [self takeSnapShot]; 
      CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); 
     } 
    } 

- 案例2:

  1. 命中按鈕(啓動定時器,拍攝快照,並行調用makeGifMethod吧)
  2. 你畫我猜
  3. 停止
  4. 檢查GIF(空GIF創建沒有任何框架,我beleieve每0.1秒調用makeGifMethod沒有按預期工作)

這裏是情況2的代碼:

-(void)recordScreen{ 

    self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES]; 

} 

-(void)takeSnapShot{ 

    //capture the screenshot of the uiimageview and save it in camera roll 
    UIGraphicsBeginImageContext(self.drawView.frame.size); 
    [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     [self makeAnimatedGif:(UIImage *)viewImage]; 

    }); 

} 


-(void) makeAnimatedGif:(UIImage *)image{ 

    NSDictionary *fileProperties = @{ 
            (__bridge id)kCGImagePropertyGIFDictionary: @{ 
              (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever 
              } 
            }; 

    NSDictionary *frameProperties = @{ 
             (__bridge id)kCGImagePropertyGIFDictionary: @{ 
               (__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data 
               } 
             }; 

    documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; 
    fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"]; 

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, 10, NULL); 
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); 

      CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); 

    if (!CGImageDestinationFinalize(destination)) { 
     NSLog(@"failed to finalize image destination"); 
    } 
    CFRelease(destination); 

    NSLog(@"url=%@", fileURL); 
} 

可有人請建議我如何拍攝的圖像通過上述方法制作GIF?

回答

4

經過一些工作,我選擇將所有捕獲的圖像存儲到數組中,並使用該數組將圖像傳遞給gifMethod。它的工作非常酷!

我已存儲的所有圖像轉換成數組:

-(void)takeSnapShot{ 

//capture the screenshot of the uiimageview and save it in camera roll 
UIGraphicsBeginImageContext(self.drawView.frame.size); 
[self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

//store all the images into array 
[imgArray adDObject:viewImage]; 

} 

注:請確保您調整圖像大小,你把它們存儲陣列別人之前,你可能最終得到內存警告,然後應用程序崩潰,如果這是用來時間更長。

後來又使用相同的陣列:

-(void)makeAnimatedGif { 
    NSUInteger kFrameCount = imgArray.count; 

    NSDictionary *fileProperties = @{ 
            (__bridge id)kCGImagePropertyGIFDictionary: @{ 
              (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever 
              } 
            }; 

    NSDictionary *frameProperties = @{ 
             (__bridge id)kCGImagePropertyGIFDictionary: @{ 
               (__bridge id)kCGImagePropertyGIFDelayTime: @0.08f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data 
               } 
             }; 

    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; 
    NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"]; 

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL); 
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties); 

    for (NSUInteger i = 0; i < kFrameCount; i++) { 
     @autoreleasepool { 
      UIImage *image =[imgArray objectAtIndex:i]; //Here is the change 
      CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); 
     } 
    } 

    if (!CGImageDestinationFinalize(destination)) { 
     NSLog(@"failed to finalize image destination"); 
    } 
    CFRelease(destination); 

    NSLog(@"url=%@", fileURL); 

    } 
+1

能請你可能在你用來解決這個問題的代碼詳細點嗎?你說你把一個數組傳遞給了gifMethod,但是你在哪裏使用它? –

+0

您可以請詳細說明代碼片段。 – iMHitesh

+0

更新了答案@Hiteshsurani –