iphone - 图像保存在模拟器图库中,但不保存在 iPhone 图库中

标签 iphone ios

尝试使用下面的代码将图像保存在 iphone 画廊中,但在模拟器中工作正常,而不是在 iphone 中。

-(void)saveTkt
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(img);
[data writeToFile:@"f.png" atomically:YES];

UIImageWriteToSavedPhotosAlbum(img, self, @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), nil);
}

编辑后的代码

-(void)saveTkt
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(img);
[data writeToFile:@"f.png" atomically:YES];

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status != ALAuthorizationStatusAuthorized) {
    //show alert for asking the user to give permission

}

//UIImageWriteToSavedPhotosAlbum(img, self, @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), nil);

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

[library writeImageToSavedPhotosAlbum:[img CGImage] orientation:(ALAssetOrientation)[img imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
        // TODO: error handling
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    } else {
        // TODO: success handling
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"alert" message:@"Saved suceesfully in photos album." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}];

ALAssetsLibraryGroupsEnumerationResultsBlock assetGroupEnumerator =
^(ALAssetsGroup *assetGroup, BOOL *stop) {
    if (assetGroup != nil) {
        // do somthing
    }
};

ALAssetsLibraryAccessFailureBlock assetFailureBlock = ^(NSError *error) {
    NSLog(@"Error enumerating photos: %@",[error description]);

};

NSUInteger groupTypes = ALAssetsGroupAll;

[library enumerateGroupsWithTypes:groupTypes usingBlock:assetGroupEnumerator failureBlock:assetFailureBlock];

}

最佳答案

添加框架 Assets 库

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

[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// TODO: error handling
} else {
// TODO: success handling
}
}];
[library release];

关于iphone - 图像保存在模拟器图库中,但不保存在 iPhone 图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441559/

相关文章:

iphone - 以编程方式更改 iPhone 上的背光亮度

android - 如何在 IOS 中像 android 一样设置可见性 GONE?

iphone - 如何在 Library 目录中创建一个私有(private)目录并为其指定应用程序包 ID 的名称?

iphone - 使用 Time Profiler 分析的审查方法

iphone - Cocoa Touch 从核心数据更新 UITableView

iphone - UISLider 设置 thumb/minimumTrack/maximumTrack tint color 获取异常

iphone - 如何在 iphone 中使用圆角标签,UILabel Round Corners

ios - 事件指示器动画不起作用 - Swift

ios - 将 AVFoundation 与高分辨率视频一起使用时出现 -11855 错误代码。我应该使用什么分辨率?

ios - 当我返回 TableViewController 时出错(快速)