将 UIIMage 中显示的照片自动保存到目录

Posted

技术标签:

【中文标题】将 UIIMage 中显示的照片自动保存到目录【英文标题】:saving photo automatically that is displayed in a UIIMage to a directory 【发布时间】:2013-08-15 03:04:04 【问题描述】:

我想将照片保存在目录中。但是,我在视图控制器中使用此代码创建了目录。

  NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

    for (int i = 0; i < [directoryNames count] ; i++) 
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

UIImage 正在显示用户拍摄的内容。当照片显示在 UIIMage 上时,我想将照片自动保存到我制作的目录之一(例如帽子)。下面是我在拍摄照片后显示给 UIImage 的代码。是否可以自动保存到其中一个目录??

- (void) processImage:(UIImage *)image  //process captured image, crop, resize and rotate
    haveImage = YES;

    if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad)  //Device is ipad
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(768, 1022));
        [image drawInRect: CGRectMake(0, 0, 768, 1022)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 130, 768, 768);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
        //or use the UIImage wherever you like

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);

    else //Device is iphone
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(320, 426));
        [image drawInRect: CGRectMake(0, 0, 320, 426)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 55, 320, 320);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);
    

    //adjust image orientation based on device orientation
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) 
        NSLog(@"landscape left image");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
        [UIView commitAnimations];

    
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 
        NSLog(@"landscape right");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(90));
        [UIView commitAnimations];

    
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
        NSLog(@"upside down");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(180));
        [UIView commitAnimations];

    
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) 
        NSLog(@"upside upright");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
        [UIView commitAnimations];
    








- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.



- (IBAction)switchCamera:(id)sender  //switch cameras front and rear cameras
    if (cameraSwitch.selectedSegmentIndex == 0) 
        FrontCamera = YES;
        [self initializeCamera];
    
    else 
        FrontCamera = NO;
        [self initializeCamera];
    

【问题讨论】:

“自动”是什么意思? 我正在让用户拍照并显示在 UIImage 中。我希望将 UIImage 中显示的图片保存到我创建的“一个”目录中。很抱歉造成混乱。 /你想自动选择一个目录吗? 是的,正如我在创建目录的代码中所做的那样。我只想将照片保存到“正确”目录 那么你应该按照什么规则来选择一个目录? 【参考方案1】:

你可能想把它放在processImage:的底部

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:directoryNames[2]]; // "right" is at index 2, per comments & code
NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; // you maybe want to incorporate a timestamp into the name to avoid duplicates
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];

【讨论】:

我做的目录名怎么设置? folderPath 是您创建的目录,您看到了吗? filePath 只是附加一个文件名。清楚吗? “发生了什么”是什么意思? 你创建了一个目录,这段代码将数据写入该目录中的一个文件,所以它发生了什么事,它有一个文件写入它。 还有什么是正确的在索引 2?我在第一个代码上的 directoryNames 中也出现错误。

以上是关于将 UIIMage 中显示的照片自动保存到目录的主要内容,如果未能解决你的问题,请参考以下文章

将UIImage保存到iOS照片库和对应程序沙盒中

将 UIImage 数据保存到 iOS 中的照片库后取回

将 UIImage 保存为不同的名称

将 UIImage 保存到 Core Data 时出错

如何将 UIImage 保存到文档目录?

在 Android 浏览器中使用 input type=file capture=camera 上传照片时,防止将照片自动保存到图库