保存到目录不起作用

时间:2013-08-16 13:32:02

标签: ios directory save uicollectionview

我正在尝试将图像保存到我制作的目录之一。

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

        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

        NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
        NSString *filePath = [filePath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];  

然而,由于我制作了一个集合视图,所以它无法正常工作,因此我可以看到保存的内容。如果问的话,我会展示它的代码。另外我收到警告说在我的代码末尾说未使用的变量文件夹路径和变量文件路径在其自己的初始化中使用时未初始化。我的最终目标是用户在目录帽子中拍照。它显示在从目录帽子获取照片的集合视图中。我的代码中有任何缺陷吗?

1 个答案:

答案 0 :(得分:0)

你这样做:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
NSString *filePath = [filePath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 

当我认为你的意思是这样做时:

NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"];

基本上,您有folderPath未使用的变量警告,因为您错误地写了NSString *filePath = [filePath ...,它会完全忽略文件夹路径,创建一个空字符串,然后将您的文件名附加到它。如果您记录了此字符串,您会看到它只输出您的文件名,而没有前面附加文档目录的路径。