iPhone 7
iPhone 7

Reputation: 1741

image getting saved in simulator gallery but not in iphone gallery

Trying to save the image in iphone gallery with following code below, but working fine in simulator, not in 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);
}

edited code

-(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];

}

Upvotes: 0

Views: 374

Answers (3)

Gaurav Patel
Gaurav Patel

Reputation: 957

Add Framework assetsLibrary

 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];

Upvotes: 1

Kirtikumar A.
Kirtikumar A.

Reputation: 4204

I think you should try with NSData first if you need It, or you can simply do that without NSData

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

Hope ,it will work

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

Apple's documentation say that the completion handler must conform to this:

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

And there's an error parameter passed along, which you should print out to see what the actual true error is.

For example:

- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error != NULL)
    {
        NSLog( @"error while saving image is %@", [error localizedDescription]);
    }
}

Upvotes: 0

Related Questions