gpt4 book ai didi

ios - 将图像文件从 Parse 保存到 iPhone

转载 作者:行者123 更新时间:2023-11-29 02:41:27 24 4
gpt4 key购买 nike

我有一个名为 data 的解析类,其中包含图像。我的 DetailViewController 包含一个保存来自 data 类的图像的 PFImageView 和一个名为 saveButton< 的 UIButton/

我想将该图像保存到用户 iPhone 上的新文件夹中。我找到了一些相关的答案,但无法成功利用它们。

@property (nonatomic, strong) PFFile *image;
@property (nonatomic, strong) UIImage *imageToSave;

- (void) getImageObject {
// I'm getting the image here, that I want to download and save to the device
PFQuery *queryObject = [PFQuery queryWithClassName:@"data"];
[queryObject whereKey:@"objectId" equalTo:self.objectIdent];
[queryObject getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

PFFile *file = [object objectForKey:@"img"];
// load to the PFImageView
self.bigImage.file = file;
[self.bigImage loadInBackground];

self.image = file;
}];
}
// Download and save image to new folder
- (void) getImageFromParse {

[self.image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.png"];
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:fileName atomically:YES];
NSLog(@"dev log 2");
}
}];
}
- (IBAction)saveButton:(id)sender {

[self getImageFromParse];
}

此代码不会产生崩溃,只是什么也没有发生(开发日志 2 出现在控制台中),所以我不知道为什么不运行它或在出错时崩溃。我真的很感激任何可以帮助我解决这个问题的指导或建议。

几乎可以工作的版本,基于 walle84 的回答:(唯一的问题是,它将图像保存到相机胶卷而不是新文件夹)

  [self.image getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];

//Saving to app folder
NSData *webData = UIImagePNGRepresentation(image);
[webData writeToFile:@"my new folders name" atomically:YES];
//imagePath would your app folder
//OR if album of iphone then below
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (!error) {

}
// Success
}];
}];

最佳答案

您可以使用以下代码从解析中获取图像。

 PFQuery *query = [PFQuery queryWithClassName:@"Your_Class_Name"];
[query getObjectInBackgroundWithId:@"OBJECT_ID" block:^(PFObject *pfObj, NSError *error)
{
if (!error) {
PFFile *imageFile = [pfObj objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data]; //Got your image

//Saving to app folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *imagePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name to document directory
NSData *webData = UIImagePNGRepresentation(image);
[webData writeToFile:imagePath atomically:YES]; //imagePath would your app folder
//OR if album of iphone then below
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (!error)
// Success
}];

//Best way to create your own folder in album of iphone/ipad.
[libraryFolder addAssetsGroupAlbumWithName:@"YOUR_ALBUM_NAME" resultBlock:^(ALAssetsGroup *group)
{
NSLog(@"Added folder");
}failureBlock:^(NSError *error)
{
NSLog(@"Error");
}];
}
}
}];

你也可以看看 documentation .

关于ios - 将图像文件从 Parse 保存到 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25733177/

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