购买时,从捆绑中将图像添加到照片库

时间:2014-06-26 15:11:33

标签: iphone objective-c cocos2d-iphone

在templeRun中,我们也希望添加inAp购买图片。

http://www.youtube.com/watch?v=dYog6yhs0y4

如何将游戏包中放置的图像下载到iphone的照片库?

2 个答案:

答案 0 :(得分:1)

使用UIImage从捆绑包中的图片中创建imageNamed:对象,然后使用UIImageWriteToSavedPhotosAlbum()将其保存到照片库。

UIImage *image = [UIImage imageNamed:@"wallpaper.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

有关UIImageWriteToSavedPhotosAlbum()(回调函数等)选项的更多信息,您可以查看UIKit Function Reference

答案 1 :(得分:1)

调用UIImageWriteToSavedPhotosAlbum,我相信看起来像这样:

UIImageWriteToSavedPhotosAlbum(
   UIImage*  image,
   id        completionTarget,
   SEL       completionSelector,
   void*     contextInfo
);

使用它的一种方法是:

UIImage* myImage = [UIImage imageNamed:@"image.png"];

UIImageWriteToSavedPhotosAlbum(myImage, self, @selector(image:didFinishSavingWithError:contextInfo), nil);

如果您希望在成功或失败时收到通知,则只需设置完成目标和完成选择器。如果是这样,请不要忘记将此方法添加到课程中:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo
{
    if (error)
    {
        // Do something on error... (like show a UIAlert for example)
    }
    else
    {
        // Do something on success... (maybe show a "Your image was saved" alert
    }
}

图像不会立即保存,并且可能会在过程中出错,因此这可能很有用。

你也可以使用ALAssetsLibrary,我相信它会是这样的:

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:[myImage CGImage] orientation:(ALAssetOrientation)[myImage imageOrientation] completionBlock:^(NSURL* assetURL, NSError* error)
{
    if (error)
    {
        // Do something on error... (like show a UIAlert for example)
    }
    else
    {
        // Do something on success... (maybe show a "Your image was saved" alert
    }
}];

希望这会有所帮助。