【问题标题】:How to show images that are stored in document directory to iphone gallery?如何将存储在文档目录中的图像显示到 iphone 画廊?
【发布时间】:2014-09-14 11:54:37
【问题描述】:

我正在截取我的视图并将其存储在文档目录中。但我需要在 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();
}

【问题讨论】:

    标签: objective-c iphone ios7 nsdocumentdirectory


    【解决方案1】:

    在这里,我在另一个视图中传递了字符串变量,与您可以用于图像的方式相同。 图像 代码如下:

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

    【讨论】:

      【解决方案2】:

      如果您需要在 iPhone Photo App 中查看图像,则必须将图像存储在相册中,请使用此代码

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

      【讨论】:

        【解决方案3】:

        你可以使用这个功能:

        UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                                   id completionTarget, 
                                   SEL completionSelector, 
                                   void *contextInfo);
        

        如果要在 UIImage 保存完成时收到通知,则只需要 completionTarget、completionSelector 和 contextInfo,否则可以传入 nil。

        例如:

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

        【讨论】:

        • @muthukumaresh 你应该接受答案并让其他人知道它工作正常
        【解决方案4】:

        使用以下代码将图像存储到图库中

        UIImageWriteToSavedPhotosAlbum(imgNewImage, nil, nil, nil);
        

        imgNewImage 是您的 UIImage 对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-19
          • 1970-01-01
          • 2012-07-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-06
          相关资源
          最近更新 更多