0

I am using the following code to capture the UIView as image

-(void)startSnapSot {
    NSLog(@"TakeSnapShot");

        UIImage* image = nil;

        //   image = [self screenshot];
        UIGraphicsBeginImageContext(overlayView.frame.size);
        {
            [overlayView.layer renderInContext: UIGraphicsGetCurrentContext()];
            image = UIGraphicsGetImageFromCurrentImageContext();
        }
        UIGraphicsEndImageContext();

        ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
            if (error) {
                NSLog(@"Error Saving Image");
            }
            else {
                NSLog(@"Image Saved");
            }
        };

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage]
                                  orientation:(ALAssetOrientation)[image imageOrientation]
                              completionBlock:completionBlock];
}

But the problem is that it is saving the first image again and again i.e. it is saving same image over again and again rather than capturing different UIIView image.Could anybody let me know what wrong am I doing?

  • Add `UIView` or `CGLayer` parameter to `startSnapSot` method to make execution more clear, you are probably forgetting to change the reference at `overlayView` var. – A-Live Nov 07 '13 at 13:23
  • overlayView is declared global.So,I don't need to pass the parameter to the startSnapSot method.It would be taken automatically. – user1851271 Nov 07 '13 at 13:37
  • That is what I said, you are probably not changing its value before calling the method next time, that is one of the reasons why using global variables at method is a bad practice. If you use parameter instead, it will be more safe and clear. – A-Live Nov 07 '13 at 13:58

0 Answers0