1

我编写了一些代码来制作视图的屏幕截图。我将该图像写入照片库。但问题是,我想在另一个 ViewController 的另一个 imageView 中使用该图像。如何将图像保存在应用程序的某个位置并在另一个 ViewController 中使用?

我的代码:

UIView* captureView = self.view;

UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect cropRect = CGRectMake(0 ,0 ,640,1136);
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);
4

3 回答 3

1

您首先需要将图像保存在磁盘上:

NSString *documentDirectory = 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
                                                                        objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/myImage.png",documentDirectory];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(customScreenShot)];
[imageData writeToFile:pngFilePath atomically:YES];

然后您可以使用该文件创建一个 UIImageView:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage 
imageWithContentsOfFile:pngFilePath]];
[self.view addSubview:myImageView];
于 2013-05-24T15:18:58.873 回答
0

如果您使用的是 Storyboard,您可以像这样将它传递给下一个视图控制器;在 prepareForSegue 方法中(这使您不必将其保存到磁盘):

注意:segue 的名称由您在 MainStoryboard.storyboard 中设置。- mySegue

kMySegue只是一个关键。IE#define kMySegue @"mySegue"

imageInOtherViewController是另一个视图控制器中的 UIImage。

// TheFirstViewController.m

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    id destinationViewController = [segue destinationViewController];

    if ([[segue identifier] isEqualToString:kMySegue]){
        if ([destinationViewController respondsToSelector:@selector(setImageInOtherViewController:)]){
            [destinationViewController setImageInOtherViewController:[UIImage imageNamed:@"myImage.png"]];
            FastCameraViewController *viewController = segue.destinationViewController;
            viewController.delegate = self;
        }   
    } 


    // OtherViewController.h

    @interface OtherViewController : UIViewController 
    {
    }
    @property (nonatomic, strong) IBOutlet UIImageView *otherViewControllerImageView;
    @property (nonatomic, strong) UIImage *imageInOtherViewController;

    // OtherViewController.m
    @implementation OtherViewController
    @synthesize otherViewControllerImageView;
    @synthesize imageInOtherViewController;

    - (void)viewDidLoad{
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

        [[self otherViewControllerImageView] setImage:imageInOtherViewController];

   }
于 2013-05-24T15:21:55.070 回答
0

要缓存您的图像,我建议首先使用NSCache类,即

NSCache* imagesCache = [[NSCache alloc] init];
[imagesCache setName:@"imagesCache"];

[imagesCache setObject:customScreenShot forKey:@"image1"];

好消息是它NSCache接受任何类型的对象,id因此您可以存储图像、mp3 以及任何您想要的东西。

如果您想使用手机的文件目录(使用起来比NSCache..更昂贵)我建议您使用Library/Caches目录而不是Documents目录..所以建立在 iDevzilla 的回答

NSString *documentDirectory =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
 objectAtIndex:0];
于 2013-05-24T16:47:17.133 回答