2013-08-15 5 views
0

디렉토리에 사진을 저장하고 싶습니다. 그러나 뷰 컨트롤러에서이 코드를 사용하여 모자 바닥이라고하는 디렉토리를 오른쪽 및 왼쪽으로 만들었습니다.특정 디렉토리에 사진 저장

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.Below에 표시 될 때 내가 만든 디렉토리 (예 : 모자) 중 하나에 자동으로 사진을 저장하고 싶습니다. 사진을 찍은 후 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]; 
    } 
} 


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

답변

0

자동으로 당신이해야하는 경우 명시 적으로 사용자에게 묻지 않고 절약 절약 UPDATE. 이미지를 저장하는 과정은 동일하다 :


( writeToFile:atomically: 사용) ( UIImageJpegRepresentation 또는 UIImagePngRepresentation 사용) 데이터
  • 저장 데이터로 변환 UIImage
  • 의 인스턴스를 가져옵니다

    다음과 같음 :

    folderPath = [documentsDirectory stringByAppendingPathComponent:directoryNames[0]]; 
    
    ,

    또는

    folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; 
    
  • +0

    좋아, 나는 당신이 그러나 나는 아직도 내가 만든 디렉토리를 지정하는 방법을하지 않는 나에게 무슨 짓을. 그냥 오른쪽에 이미지를 저장하고 싶습니다. – Shouri

    +0

    질문의 맨 위에있는 코드에서 디렉토리 경로를 확인하여 디렉토리 경로가 존재하는지 확인하십시오. – Wain

    +0

    'NSString * folderPath = [NSDocumentDirectory stringByAppendingPathComponent : directoryNames [%]]; '이 뜻입니까? 나는 디렉토리 이름을 어디에 두는가? – Shouri