gpt4 book ai didi

iphone - 尝试将照片库中的图像保存到 iPad 应用程序包中

转载 作者:行者123 更新时间:2023-12-03 19:59:32 25 4
gpt4 key购买 nike

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

这是我编写的以下代码,但我无法保存图像。我只能保存名为 NULL.png 的图像。请建议进行更改以将我的库中的所有图片保存到 bundle 中。

谢谢,克里斯蒂

最佳答案

示例代码

    - (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");

}

关于iphone - 尝试将照片库中的图像保存到 iPad 应用程序包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5791125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com