I am downloading a file using the following:
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"URL HERE"]];
Displaying it with the following:
UIImage *img = [UIImage imageWithData:imgData];
And saving it to the device with the following:
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pth = [[paths objectAtIndex:0] stringByAppendingPathComponent:self.localFile];
NSData *d1 = [NSData dataWithData:UIImageJPEGRepresentation(img,1.0f)];
[d1 writeToFile:pth atomically:YES];
Photo displays fine in my UIImageView. But when I reload the application and display the file, I get the following error:
ImageIO: CGImageRead_mapData 'open' failed '/var/mobile/Applications/422122E1-3244-46CE-BB6C-123C750E2191/Documents/14741078_1.jpg'
error = 2 (No such file or directory)
ImageIO: <ERROR> JPEGNot a JPEG file: starts with 0xff 0xd9
I have just resolved the same issue.
Don't change your load method.
But save your image like this :
NSError* error = nil;
[UIImageJPEGRepresentation(img, 1.0f) writeToFile:pth options:NSDataWritingAtomic error:&error];
Related
I want to share a single image to Instagram "image.png" Is this an iOS 11 error?
I'm getting this error:
Couldn't get file size for file:/var/mobile/Containers/Data/Application/492BF71E-E579-4792-8BE9-144D9C1EC73D/Documents/insta.igo -- file:///: Error Domain=NSCocoaErrorDomain Code=260 "The file “insta.igo” couldn’t be opened because there is no such file." UserInfo={NSURL=file:/var/mobile/Containers/Data/Application/492BF71E-E579-4792-8BE9-144D9C1EC73D/Documents/insta.igo -- file:///, NSFilePath=/file:/var/mobile/Containers/Data/Application/492BF71E-E579-4792-8BE9-144D9C1EC73D/Documents/insta.igo, NSUnderlyingError=0x1cc054760 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Here's the code
-(void)shareInInstagram{
UIImage *imge = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(imge); //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:#"insta.igo"]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(#"image saved");
CGRect rect = CGRectMake(0 , 0, 0, 0);
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
NSString *fileNameToSave = [NSString stringWithFormat:#"Documents/insta.igo"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:fileNameToSave];
NSLog(#"jpg path %#",jpgPath);
NSString *newJpgPath = [NSString stringWithFormat:#"file://%#",jpgPath]; //[[NSString alloc] initWithFormat:#"file://%#", jpgPath] ];
NSLog(#"with File path %#",newJpgPath);
NSURL *igImageHookFile = [[NSURL alloc] initFileURLWithPath:newJpgPath];
NSLog(#"url Path %#",igImageHookFile);
self.dic.UTI = #"com.instagram.exclusivegram";
self.dic = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.dic=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
}
This code works in my app.
GRect rect = CGRectMake(0 ,0 , 0, 0);
UIImage *image = [self getImageToShare];
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 *jpgPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"insta.igo"]]; //add our image to the path
[fileManager createFileAtPath:jpgPath contents:imageData attributes:nil]; //finally save the path (image)
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
self.documentController.UTI = #"com.instagram.exclusivegram";
self.documentController = [self setupControllerWithURL:igImageHookFile usingDelegate:self];
self.documentController=[UIDocumentInteractionController interactionControllerWithURL:igImageHookFile];
[self.documentController presentOpenInMenuFromRect:rect inView: self.view animated: YES ];
NSURL *instagramURL = [NSURL URLWithString:#"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[self.documentController presentOpenInMenuFromRect:rect inView:self.view animated:YES ];
}else{
[self showMessage:#"No Instagram Found"];
}
I have data from a url (.m4a) and I am trying to save the file so I can edit it's metadata (change background image). The code below doesn't seem to work and I have no idea why. It is saving a file, but the file is empty.
_previewData = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:#"http://a281.phobos.apple.com/us/r1000/119/Music/v4/f1/7b/d6/f17bd6e3-55c0-b7e0-9863-bc522900e950/mzaf_5153970109972844579.aac.m4a"]];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: #"file.mp4"]];
NSFileManager *filemanager;
filemanager = [NSFileManager defaultManager];
[_previewData writeToFile:path atomically:YES];
if ([filemanager fileExistsAtPath:path]) {
// Runs
NSLog(#"It worked");
}
NSLog(#"%#",path);
ANMovie* file = [[ANMovie alloc] initWithFile:path]; // or .mp4
NSData* jpegCover = UIImageJPEGRepresentation(artworkImage, 1.0);
_imageView.image = [UIImage imageWithData:jpegCover];
ANMetadata* metadata = [[ANMetadata alloc] init];
metadata.albumCover = [[ANMetadataImage alloc] initWithImageData:jpegCover type:ANMetadataImageTypeJPG];
[file setMovieMetadata:metadata];
[file close];
_previewData = [NSMutableData dataWithContentsOfFile:path];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSDictionary *imageItem=#{#"public.mpeg-4-audio":self.previewData};
NSDictionary *textItem=#{#"public.plain-text":self.linkData};
pasteboard.items=#[imageItem,textItem];
I am trying to read NSData from a file path, and eventually unarchive the data.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
[[NSFileManager defaultManager] createDirectoryAtPath:[docs stringByAppendingString:#"/dynSplash"]
withIntermediateDirectories:NO
attributes:nil
error:nil];
NSString *dynamicSplashFile = [docs stringByAppendingString:#"/dynSplash/dynamicSplash.data"];
DynamicSplash *splash;
if([[NSFileManager defaultManager] fileExistsAtPath:dynamicSplashFile]) {
NSLog(#"Dynamic splash exists");
NSData *data = [NSData dataWithContentsOfFile:dynamicSplashFile];
splash = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
The problem arises on the line [NSData dataWithContentsOfFile:dynamicSplashFile] where a EXC_BAD_ACCESS error is bring thrown.
How can I debug this?
I saved an image in the documents directory, but when I tried to read it, it appears that the file wasnt saved at all.
The read path is OK, the same as the save one.
[_IDBanners addObject:#"44"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString * name = [NSString stringWithFormat:#"%#.png",[_IDBanners objectAtIndex:i]];
NSString *filePath = [documentsPath stringByAppendingPathComponent:name]; //Add the file name
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:pngData];
When I log:
NSLog(#"%#",image);
it returns (null).
When I save image to file it save with wrong orientation .
- (void)saveImage:(UIImage *)image:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"%#.png", imageName]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
}
Thanks.
Try saving as a jpeg instead:
NSData *imageData = UIImageJPEGRepresentation(image,1.0);
If you need to save it as a PNG, check this discussion:
UIImagePNGRepresentation ..... writeToFile is always landscape