0

I have a subclass of UIView containing a written advertising, and I want add it on a photo in a Graphics Context, but this UIView not saved in the photo Album when I save the photo. This is the code:

 - (IBAction)saveAction:(id)sender{


CGRect newFrame=imageInImageViewFrame(self.userImageView.bounds,self.userImageView.bounds.size);


CGSize imageSize=self.userImageView.bounds.size;

if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

UIGraphicsPushContext(context);

[self.userImageView.image drawInRect: newFrame];
self.banner =[[Banner alloc]initWithFrame:CGRectMake(0, 324, 320, 48)];//this is the UIView that I want add on photo

[self.userImageView addSubview:self.banner];
NSLog(@"The image is %@", self.banner);

UIGraphicsPopContext();
[self renderView:self.gestureView inContext:context ];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);

UIGraphicsEndImageContext();

NSString *docPath=(NSString*)[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagesPath=[NSString stringWithFormat:@"%@/%@/%@",docPath, self.profileName,imageDirectory];
     //trovare progressivo forse con enumerazione di directory
[[NSFileManager defaultManager]createDirectoryAtPath:imagesPath withIntermediateDirectories:NO attributes:nil error:nil]; //crazione di una directory a un certo indirizzo
NSData *imageData=UIImageJPEGRepresentation(screenshot, 0.9);
NSMutableArray *dirContent=[[[NSFileManager defaultManager]contentsOfDirectoryAtPath:imagesPath error:nil]mutableCopy]; //copia del contenuto della directory
[dirContent removeObject:@".DS_Store"]; //rimuove il file nascosto
NSArray *sortedFileNames=[dirContent sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *lastFileName=[sortedFileNames lastObject];
[dirContent release];

int progressiveImageNumber=[[[lastFileName componentsSeparatedByString:@"_"]objectAtIndex:1]intValue];
NSString *fullImageNamePath=[NSString   stringWithFormat:@"%@/photoImage_%.2d.jpg",imagesPath,progressiveImageNumber+1];
NSLog(@"fullimagepath:%@",fullImageNamePath);

[imageData writeToFile:fullImageNamePath atomically:YES];     

}

The photo is saved but the UIView on not saved. How can I do? Thanks in advance

STW
  • 44,917
  • 17
  • 105
  • 161
Ortensia C.
  • 4,666
  • 11
  • 43
  • 70

1 Answers1

0

Use this code in your method by removing yours code:

 UIGraphicsBeginImageContext(self.gestureView.bounds.size);
 [self.gestureView.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 // save image to photos
 UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil);
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132