我指的是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。
工作流程:
- 啓動應用程序。上的按鈕
- 輕按該啓動定時器,並採取圖片爲每0.1秒
- 平局與手指(所以所有附圖將被捕獲埃夫0.1秒)
- 每個快照後,我請
makeanimatedgif
的方法,所以將要加入到GIF前一幀拍攝的快照文件 - 停止一切
問題:
- 情況1:
- 按鈕點擊(它創建GIF immediatley)
- 開始繪製
- 停止
- 檢查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:
- 命中按鈕(啓動定時器,拍攝快照,並行調用makeGifMethod吧)
- 你畫我猜
- 停止
- 檢查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?
能請你可能在你用來解決這個問題的代碼詳細點嗎?你說你把一個數組傳遞給了gifMethod,但是你在哪裏使用它? –
您可以請詳細說明代碼片段。 – iMHitesh
更新了答案@Hiteshsurani –