이미지가 들어있는 data
이라는 구문 분석 클래스가 있습니다. 내 DetailViewController
에는 data
클래스의 이미지를 보유하는 PFImageView
과 saveButton
이라는 UIButton
이 포함되어 있습니다.이미지 파일을 구문 분석에서 iPhone으로 저장
그 이미지를 사용자의 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];
}
이 코드는 단순히 아무것도 (dev에 로그 콘솔에서이 개 나타납니다) 발생하지 않습니다, 충돌이 발생하지 않기 때문에 나는 그것이 잘못된 경우를 실행하거나 충돌하지 않는 이유 어떤 생각이 없습니다. 이 문제를 해결하는 데 도움이되는 지침이나 제안을 정말 고맙게 생각합니다.
거의 작업 버전, 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
}];
}];
이미지를 얻었지만 문제는 아닙니다. 문제는 장치에 저장할 수 없다는 것입니다. – rihe
앱의 앨범이나 coredata에 저장 하시겠습니까? – nikhil84
나는 내 게시물을 편집 했으므로 지금 살펴보고 필요한 것이 있으면 알려주십시오. – nikhil84