0

以下のコードで画像を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];

}
4

3 に答える 3

1

フレームワーク資産ライブラリを追加

 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];
于 2013-07-03T07:15:24.417 に答える
0

必要な場合は、最初に NSData を試す必要があると思います。または、NSData なしで簡単に実行できます。

NSData *dataImage = UIImageJPEGRepresentation(file,1);
UIImageView *img;
img.image = [[UIImage alloc] initWithData:dataImage];
 UIImageWriteToSavedPhotosAlbum( [[UIImage alloc] initWithData:dataImage], nil, nil, nil);

うまくいくことを願っています

于 2013-07-03T07:11:15.167 に答える
0

Apple のドキュメントによると、完了ハンドラはこれに準拠する必要があります。

- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;

また、渡されたエラー パラメータがあります。これを出力して、実際の真のエラーを確認する必要があります。

例えば:

- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error != NULL)
    {
        NSLog( @"error while saving image is %@", [error localizedDescription]);
    }
}
于 2013-07-03T07:03:57.207 に答える