2011-04-26 110 views
3
-(IBAction)saveImage{ 



    NSMutableArray *dictWords= [[NSMutableArray alloc]initWithObjects:@"TIGER",@"CAT", @"ROSE", @"ELEPHANT",@"MOUSE IS LOOKING FOR THE CHEESE",@"KITE",@"CAR",@"AEROPLANE",@"MANGO",@"FRUITS ARE FALLING FROM THE TREE",@"MOUNTAIN",@"BIRDS ARE FLYING",@"IGLOO",@"THIS HOUSE IS BUILT OF WOODS",@"BANANA",@"RAINBOW",@"TRAIN",@"DADDY DRINKS JUICE",@"UMBRELLA",@"GOAT",@"CAT JUMPS HIGH",@"DOG RUNS FAST",@"BUS",@"GIRL IS CRYING",@"STARS",@"DOLPHIN",@"BOYS ARE PLAYING FOOTBALL",@"GLASS IS FULL OF WATER",@"SHIP",@"SNOWFALL",@"GHOST",@"RABBIT",@"WATERMELON",@"SPIDERMAN",@"DINOSAUR",@"MICKEY MOUSE",@"MONKEY IS SITTING ON A TREE",@"PEACOCK",@"LIGHTNING",@"HEN LAYS EGGS",nil]; 
    NSString *path=[[NSBundle mainBundle] pathForResource:[youSaid text] ofType:@"png" inDirectory:@"Image"]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    for (int i=0 ; i<[assets count]; i++) {   
     if (i<[dictWords count]) {   
      [dict setObject:[[[assets objectAtIndex:i] defaultRepresentation] url] forKey:[NSString stringWithFormat:@"%@",[dictWords objectAtIndex:i]]];  
      NSLog(@"diccount:%d",[dict count]); 
     } 
    } 

    NSURL *imageurl = [dict objectForKey:[youSaid text]];  
    //NSLog(@"text:%@",[youSaid text]); 
    //Getting asset from url 
    typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset); 
    typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error); 

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) 
    { 
     ALAssetRepresentation *rep = [myasset defaultRepresentation]; 
     CGImageRef iref = [rep fullResolutionImage]; 
     //Setting asset image to image view according to the image url 
     [imageview setImage:[UIImage imageWithCGImage:iref]]; 
     youSaid.text = [NSString stringWithFormat:@"%@",imageurl]; 



    }; 
    ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) 
    { 
     NSLog(@"Error, cant get image - %@",[myerror localizedDescription]); 
    }; 
    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init]; 
    [assetLibrary assetForURL:imageurl resultBlock:resultblock failureBlock:failureblock]; 

    UIImage *image=[[UIImage alloc]initWithContentsOfFile:path]; 




NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 

NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 

NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", youSaid.text]]; //add our image to the path 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the image 

NSLog(@"image saved"); 
} 

@end 

這是我寫的以下代碼,但我不能保存images.i我只能夠理解一個圖像被稱爲NULL.png .please建議更改以保存我的圖書館中的所有圖片進行捆綁。試圖從照片庫保存圖像在ipad應用程序捆綁

感謝, 克里斯蒂

+0

錯誤請幫助 – Christina 2011-04-26 13:39:07

+0

嗨克里斯蒂娜,你檢查的UIImage *圖像爲零或不?和imageData是否爲零? – 2011-04-26 13:40:18

+0

我認爲這是零,這是問題地獄,我很困惑,從圖像獲取圖像圖像,可以üPLZ告訴我, – Christina 2011-04-26 13:50:39

回答

2

示例代碼

- (void) openPhotoLibrary { 
     NSLog(@"openPhotolibrary"); 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 
      if (self.picker != nil) { 
       NSLog(@"releasing self.picker..."); 
       [self.picker release]; 
      } 
      self.picker = [[UIImagePickerController alloc] init]; 
      self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      //self.picker.allowsImageEditing = YES; 
      [self.picker presentModalViewController:self.picker animated:YES]; 
      self.picker.delegate = self; 

      [[[CCDirector sharedDirector] openGLView] addSubview:self.picker.view]; 
     } 
    } 

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)currentPicker { 
     NSLog(@"imagePickerControllerDidCancel"); 

     // hide the self.picker if user cancels picking an image. 
     [currentPicker dismissModalViewControllerAnimated:YES]; 
     self.picker.view.hidden = YES; 
     [self.picker.view removeFromSuperview]; 
    } 

    - (void)imagePickerController:(UIImagePickerController *)currentPicker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
    { 
     [self saveImage:image withImageName:@"myPhoto"]; 

    } 

//saving an image 

- (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName { 

NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 

NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 

NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) 

NSLog(@"image saved"); 

} 
+0

但它的照片不適合視頻 – Shima 2011-07-20 11:38:16

0

此代碼的工作,我嘗試它可能是它可以幫助你

-(IBAction)PhotoLibraryButtonClicked:(id)sender{ 
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ 


    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:imagePicker animated:YES]; 

} 
else{ 

    [self displaysorceError]; 

    } 

} 


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 

//[self.popoverrcontroller dismissPopoverAnimated:true]; 
//[popoverrcontroller release]; 

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
[self dismissModalViewControllerAnimated:YES]; 

if([mediaType isEqualToString:(NSString *)kUTTypeImage]){ 

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
// UIImage *rote = [image rotateInDegrees:219.0f]; 

    img.image = image; 
    if(newMedia) 
     UIImageWriteToSavedPhotosAlbum(image, nil,nil, nil); 
}else if([mediaType isEqualToString:(NSString *)kUTTypeImage]){ 

    //not available vidio 
} 
} 


-(IBAction)saveImageIn:(id)sender{ 

NSData *imageData = UIImagePNGRepresentation(img.image); 

NSFileManager *fileMan = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@" %d.png",img.image]]; 

[fileMan createFileAtPath:fullPathToFile contents:imageData attributes:nil]; 



UIAlertView *alt = [[UIAlertView alloc] 
        initWithTitle:@"Thank You" 
        message:@"Image Save In Directory" 
        delegate:nil cancelButtonTitle:@"OK" 
        otherButtonTitles:nil]; 
[alt show]; 
[alt release]; 
} 

謝謝.. :)啃老族

0

**將photolibrary中的uiimage保存至Bundle **

。 (空隙)imagePickerController:(的UIImagePickerController *)選擇器didFinishPickingImage:(的UIImage *)圖像editingInfo:(的NSDictionary *)editingInfo {

[self dismissModalViewControllerAnimated:YES]; 


    imgview =[[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 100, 100)]; 

    imgview.image =image; 
    [self.view addSubview:imgview]; 
    [imgview setUserInteractionEnabled:YES]; 

}

(IBAction爲)saveimg:(ID)發送方{

NSData *imageData = UIImageJPEGRepresentation(imgview.image,1.0); 

NSFileManager *fileMan = [NSFileManager defaultManager]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"deepu22%d.jpg",imgview.image]]; 

[fileMan createFileAtPath:fullPathToFile contents:imageData attributes:nil]; 



UIAlertView *alt = [[UIAlertView alloc] 
        initWithTitle:@"Thank You" 
        message:@"Image Save In Directory" 
        delegate:nil cancelButtonTitle:@"OK" 
        otherButtonTitles:nil]; 
[alt show]; 

}

。 (IBAction爲)PIC * KERS:(ID)發送{

picker =[[UIImagePickerController alloc]init]; 

    picker.delegate=self; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentModalViewController:picker animated:YES]; 

} *

相關問題