0

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

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

但是它不起作用,因为我制作了一个集合视图,以便我可以看到保存的内容。如果要求,我会显示它的代码。此外,我在代码末尾收到警告说未使用的变量文件夹路径和变量文件路径在其自己的初始化中使用时未初始化。我的最终目标是用户在目录帽中拍照。它显示在从目录帽子获取照片的集合视图中。我的代码有任何缺陷吗?

4

1 回答 1

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 ...了完全忽略文件夹路径,创建一个空白字符串,然后将你的文件名附加到它。如果你记录了这个字符串,你会看到它只输出你的文件名,没有附加到文件目录的路径。

于 2013-08-16T14:13:10.080 回答