ドキュメント フォルダに gif があります。アプリのユーザーが、gif アニメーションをサポートする他のプラットフォームでアクセスして電子メールで使用できるようにしたいと考えています。
この投稿の情報を使用してgifファイルを生成しました...
iOS 経由でアニメーション GIF を作成してエクスポートしますか?
gif ファイルが生成され、正しくアニメーション化されます (サファリのシミュレーターのドキュメント フォルダーから直接開かれます)。
残念ながら、ALAssetsLibrary から UIImageWriteToSavedPhotosAlbum または writeImageDataToSavedPhotosAlbum を使用してファイルをカメラ ロールに移動しようとすると (ユーザーが簡単に電子メールで送信できるようにするため)、画像が jpg ファイルに変換され、すべてのアニメーションが失われます。
カメラロールからファイルを電子メールで送信し、別のプラットフォームで開くことでこれを確認しました(ユーザーに持たせたい機能)。
私は見つけることができるすべての投稿を読みましたが、私が見たところ、GIFファイルをブラウザからカメラロールに直接保存することが可能であるように思われます。アニメーション化されていなくても、別のプログラムで開いたときにそのプロパティを保持するので、私がやろうとしていることが少なくとも可能であることを願っています:)
助けてくれてありがとう、私のgif作成と失敗したコピー試行を以下に含めてください..
- (void) makeGifFile {
////////////////////
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
///////////////////
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @0.06f, // 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, self.screenshotnumber, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);
/////////////////////////////////
for (NSUInteger i = 1; i < self.screenshotnumber+1; i++) {
@autoreleasepool {
////
NSString *name = [NSString stringWithFormat: @"Screenshot%d", i];
NSString *pathstring = [NSString stringWithFormat: @"Documents/%@.png", name];
NSString *gifPath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];
/////
UIImage *image = [UIImage imageWithContentsOfFile:gifPath];
CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties); }
}
///////////////////////////////////////
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
NSLog(@"url=%@", fileURL);
//////////////////////////////
///
/// saved in documents directory
/////////////////////////////
//////////////////////////
//////
///// now move to camera roll
///////////////////////
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSString *documentDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *gifImagePath = [NSString stringWithFormat:@"%@/%@", documentDirectory, @"animated.gif"];
UIImage *gifImage = [UIImage imageWithContentsOfFile:gifImagePath];
UIImageWriteToSavedPhotosAlbum(gifImage, nil, nil, nil);
CCLOG(@"wrote to camera roll");
//////////////////////// gets saved as JPG not gif
//////// next try...
NSData *data = [NSData dataWithContentsOfFile:gifImagePath]; // Your GIF file path which you might have saved in NSDocumentDir or NSTempDir
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Error Saving GIF to Photo Album: %@", error);
} else {
// TODO: success handling
NSLog(@"GIF Saved to %@", assetURL);
// success(gifImagePath);
}
}];
/////////// also gets saved as jpg
}
興味のある人のためにスクリーンショットを作成するための私の方法...これを見つけた投稿を見失ってしまいました...誰かが私にリンクを提供できるなら、私はここで正当な信用を与えます...
他の人に役立つ場合に備えて、関連するすべての機能をまとめて含めるために含まれています:)
-(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize viewSize = [[CCDirector sharedDirector] viewSize];
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:viewSize.width
height:viewSize.height];
[rtx begin];
[startNode visit];
[rtx end];
return [rtx getUIImage];
}
- (void) saveScreenShotWithName: (NSString*) name
{
CCScene *scene = [[CCDirector sharedDirector] runningScene];
CCNode *n = [scene.children objectAtIndex:0];
UIImage *tempimage = [self screenshotWithStartNode:n];
NSString *pathstring = [NSString stringWithFormat: @"Documents/%@.png", name];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:pathstring];
// Write image to PNG
[UIImagePNGRepresentation(tempimage) writeToFile:savePath atomically:YES];
}
単純なループでファイルが作成され、gif の作成後にドキュメント ディレクトリから別のファイルが削除されます