2014-07-23 105 views
0

我正在截取我的視圖並將其存儲在文檔目錄中。但我需要在iphone畫廊應用程序中顯示存儲的圖像。我不知道如何將圖像傳遞到圖庫視圖。 幫助我。 感謝ü在進階..如何顯示存儲在文檔目錄中的圖像到iphone圖庫?

- (IBAction)Screenshot:(id)sender { 

    CGSize targetImageSize = CGSizeMake(500, 500); 
    // Check for retina image rendering option 
    if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(targetImageSize, NO, 0); 
    else UIGraphicsBeginImageContext(targetImageSize); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    // The view to be rendered 
    [[image layer] renderInContext:context]; 
    // Get the rendered image 
    UIImage *original_image = UIGraphicsGetImageFromCurrentImageContext(); 
    NSLog(@"%@",original_image); 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/screenshots"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 
    //Create folder 
// NSString *documentsDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/myPngFile.png",dataPath]; 
    [UIImagePNGRepresentation(original_image) writeToFile:pngFilePath atomically:YES]; 
    //[_image1 setImage:original_image]; 
    UIGraphicsEndImageContext(); 
} 

回答

3

您可以使用此功能:

​​

你只需要completionTarget,completionSelector和contextInfo如果你希望在UIImage的儲存完成後得到通知,OT herwise,你可以通過零。

例如:

-(void)savePhoto { 
    NSURL *imageURL = receivedURL; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]; 
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
} 

- (void) savedPhotoImage:(UIImage *)image 
    didFinishSavingWithError:(NSError *)error 
      contextInfo:(void *)contextInfo 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
               message:@"This image has been saved to your photo album" 
               delegate:nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [alert show]; 
} 
+1

u救了我的一天(Y) –

+2

@muthukumaresh你應該接受答案並讓別人知道它工作正常 –

0

在這裏,我傳遞字符串變量中,你可以使用圖像anotherview同樣的方式。 圖像0​​代碼如下:

UserDetailsViewController *userDetailsViewController = [[UserDetailsViewController alloc] initWithNibName:@"UserDetailsViewController" bundle:nil]; 
userDetailsViewController.img = [UIImage imageNamed:@"abc.png"]; 
[self.navigationController pushViewController:userDetailsViewController animated:YES]; 
0

你必須保存在相冊的圖片,如果你需要看到它在iPhone照片應用程序使用此代碼

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    UIImage *image=//what ever Image you wan tot save and view in photo album of iPhone; 

[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[SAVEIMAGE imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ 
     if (error) { 
      // TODO: error handling 

     } else { 




      // TODO: success handling 
     } 

    }]; 
0

使用下面的存儲的圖像代碼轉換成畫廊

UIImageWriteToSavedPhotosAlbum(imgNewImage, nil, nil, nil); 

imgNewImage是你的UIImage對象。

相關問題