2012-06-12 89 views
0

我有一个包含书面广告的UIView的子类,我想将它添加到图形上下文中的照片上,但是当我保存照片时,此UIView未保存在照片相册中。这是代码:用PhotoAlbum保存照片

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

}

照片被保存,但在未保存的UIView。我能怎么做?在此先感谢

+0

照片被保存,但对UIView的未保存..不懂.. –

+0

UIView的是照片的子视图 –

回答

0

使用此代码在您的方法去除你的代码:

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

// save image to photos 
UIImageWriteToSavedPhotosAlbum(screenShot, self, nil, nil); 
+0

我必须添加self.banner作为照片的子视图 –

+0

谢谢,解决方案是:[self.banner.layer renderInContext:UIGraphicsGetCurrentContext()]; –