Get the name of an image taken from camera/photo library - iphone

I am having an issue with getting the name of an image that is taken from the photo library or camera. I have successfully picked the image but I want to send the picked image as an email attachment using MFMailComposeViewController. The problem is that the MFMailComposeViewController required a specific name of the image. I have used the following code to get the image name, and it always returning "assest.JPG" and that is not working with MFMailComposeViewController
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
[imageView setImage:image];
image_ = image;
image_ = [UIImage imageNamed:#"file.jpg"];
NSURL *imagePath = [editingInfo objectForKey:#"UIImagePickerControllerReferenceURL"];
imageName = [imagePath lastPathComponent];
NSLog(#"%#",imageName);
[self dismissModalViewControllerAnimated:YES];
if (CAMORLIB == 1) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}}
is there is any other way to deal with this issue?
Thanks,
Mohammed

Your code here:
image_ = image;
image_ = [UIImage imageNamed:#"file.jpg"];
doesn't make sense, as it assigns to image_ twice. The UIImage method -imageNamed:(NSString*)name tries to load a file named file.jpg from disk.
The reason your MFMailComposeViewController doesn't work is that the above code results in image_ being nil because [UIImage imageNamed:#"filename"] doesn't find a file named that (and as a result returns nil)
Attaching to mail
When attaching an image to a MFMailComposeViewController, you tell it what filename you want the image to have, along with the image's data:
NSData* myData = UIImageJPEGRepresentation(image, .70f);
[picker addAttachmentData:myData mimeType:#"image/jpg" fileName:#"desiredfilename.jpg"];
Here, image is a UIImage and #"desiredfilename.jpg" is the filename you want the image to appear to have in the email.

Pulling an image from the Assets Library only gives you the guid url of the item.
There is no real construct of names in the Assets Library. The photo library has a few properties but they are as follows
ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate
all of which can be found in AssetsLibrary.framework\ALAsset.h
to put a name for images I gather from there, I usually put
[NSString stringWithFormat:"Image%#.png",imageNumber]
where image number incremented as I add images.
Hope that helps

Related

How know whether image is coming from cache or form url in SDWebImage?

I am using SDWebImage for loading a list of images and it was really good but while loading images next time it's loading image from cache if the url is same. But I need to know its coming form cache or web. Any help would be greatly appreciated.
Check SDWebImageManager, it has 2 versions of functions to check whether an URL is already cached or not:
- (BOOL)diskImageExistsForURL:(NSURL *)url;
There's also a version with a block which executes after the check. Hope this helps!
You can check the cache to see if the image in question has already been downloaded like so:
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:URL];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
use your url
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
if (cachedImage)
{
[button setImage:cachedImage];
//image is cashed
}
else
{
[manager downloadWithURL:url delegate:self];
}
and then implement delegate function
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage*)image {
[button setImage:image];
// new image
}

Error when adding an image to an Mail - MFMailComposeViewController

I'm trying to send an Image, which I have added to my Project, using the MFMailComposeViewController.
I tried this in the simulator and it works fine, I can also see the Image in the Textfield, but when I'm trying to send the Mail, I get the following error on console:
Feb 10 16:22:02 markuss-macbook-pro.speedport.ip RMC Konus[1825] : CGImageCreate: invalid image size: 0 x 0.
My code:
//Get the Image
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
//Create the Mail View
MFMailComposeViewController *mailCV = [[MFMailComposeViewController alloc] init];
[mailCV setMailComposeDelegate:self];
[mailCV addAttachmentData:imageData mimeType:#"image/png" fileName:#"exGraphic.png"];
//Show the View Controller
[self presentViewController:mailCV animated:YES completion:nil];
The problems only appears in the simulator.
I check these code on many different devices and it still works without problems. So I would say, it is a problem of the simulator ...
Return Value of UIImagePNGRepresentation (from apple document)-
A data object containing the PNG data, or nil if there was a problem
generating the data. This function may return nil if the image has no
data or if the underlying CGImageRef contains data in an unsupported
bitmap format.
that means check whether your image is supported for converting it into NSData with png format, because you are receiving NULL here.your error CGImageCreate: invalid image size: 0 x 0. says that it is NULL.
check by changing image . also check without using [UIImage imageNamed:#""] because this method has many disadvantageous.
another one thing, you have converted image.png image into NSData & sending another image exGraphic.png as attachment,check it

Making a copy of a UIImage

I currently am using the code I found on an online tutorial to have the iPhone user select an image from their camera roll. Setting the image to the UIImageView works fine, but then I try to call a function on the image (every time a slider is moved). It turns out that the operating system is not holding onto the image, so when I try to access it (or the caching variable I made), it doesn't work (because the address gets set to 0x0000000).
If UIImage conformed to NSCopying, this would be simple. But, it doesn't. So, how do I copy an image so the operating system doesn't delete it?
Possible Duplicates
How do I make an exact copy of a UIImage returned from a UIImagePickerController?
Making deep copy of UIImage
EDIT:
The slider, when changed, calls this function:
- (IBAction)samplingSliderChanged:(id)sender {
NSLog(#"%#", self.imageStay);
float sample = self.samplingSlider.value * 0.6 + 0.2;
self.samplingRateText.text = [NSString stringWithFormat:#"%.0f%%", sample*100];
//self.imageView.image = [self.brain sampleImage:self.imageStay atRate:sample];
self.imageView.image = [self.brain sampleImage:self.imageStay.copy atRate:sample];
NSLog(#"self.imageStay: %#, self.imageStay.copy: %#", self.imageStay, self.imageStay.copy);
}
The first time the slider is moved, my self.imageStay (the cache variable) has an address of 0x0000.
My show-the-camera-roll function is below:
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
UIImage * image;
[self dismissViewControllerAnimated:YES completion:nil];
image = info[UIImagePickerControllerOriginalImage];
image = [self imageWithImage:image scaledToSize:CGSizeMake(256, 256)];
self.imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
self.imageView.image = image;
self.imageStay = image
}
In here, the cache variable has the same memory address as image, and my guess is that this later gets deleted.
You shouldn't be worrying about NSCopying, or making a copy of a UIImage, this isn't your problem. You are losing the reference to the image in your imageView. The OS is holding onto the image, if not it would disappear from your imageView. I expect you just aren't referring to it correctly. Perhaps you should show what you mean by a 'caching variable'...
In any case a reliable way to get to the image currently displayed in your imageView is to obtain it from the imageView's image property, as self.imageView.image.
update
If self.imageView.image might be subject to filtering, and you also need to keep a reference to the original image (eg in self.imageStay) you should declare the imageStay property as
#property (nonatomic, strong) UIImage* imageStay;
If you have done this correctly, and still have a nil reference in self.imageStay, step through your code in the debugger to see what is going on here:
self.imageView.image = image;
if (_newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
self.imageView.image = image;
self.imageStay = image
self.imageStay and self.imageView.image should now both have a reference to the same object. By the way, you second assignment of self.imageView.image = image here is redundant.
Then in - (IBAction)samplingSliderChanged:(id)sender you should find those references persisting.
Your problem might be caused if you have declared your #property imageStay as a weak reference. It should be strong to ensure that it persists.

save a photo library URL as an NSString then back to URL

what i am trying to do is save a URL to one of the photo library pictures. then use this URL to set the image on a CCSprite.
this is the URL being saved as a NSString :
[_currentTarget setObject:[[NSString alloc]initWithString:[[info objectForKey:#"UIImagePickerControllerReferenceURL"]absoluteString]] forKey:#"image1Name"];
Then set the a property of a Target object i made
NSString *image1PathName = [[listOfUserMadeTargets objectAtIndex:num]objectForKey:#"image1Name"];
self.normalImage = image1PathName;
this is when the CCSprite is made
Target *target = [[Target alloc] targetThatIsUserMade];
[_targets addObject:target]; // add it to the array
target.position = ccp(spawnX,spawnY);
target.scale = -0.001875 * target.position.y + 1.09375;
[self addChild:target z:1];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:target.normalImage]]];
[target setTexture:[[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:#"key" ]];
The problem im having is that the Target is there, but the image is not
I have done this many different ways with no luck
thank you for any help
This is a post (and answer) I made which shows you how to save the NSURL for a UIImage in the CameraRoll, and use it later to get the image back, using a UIImagePickerController. This sample code should also help you in how to access the image. Hope that Helps!

How do I read in & display a local image on UIImageView via SQLite

How may I modify, read in an image path from SQLite DB, & display it via an UIImageView? In other words, this application will work in offline mode & i have the images at hand. How will i go about this then, tgt with sqlite? (retrieve images by storing their image path name # sqlite)
Sample code I have for now:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
//start with index 0
currentStepIndex = 0;
Resolution *resolutionObj = [appDelegate.resolutionArray objectAtIndex:currentStepIndex];
[resolutionDescription setText:resolutionObj.stepDescription];
//checking for null string
//replicate of ifelse #goToNextStep & #goToLastStep (needed for initial load of image)
if (resolutionObj.imageDescription != NULL)
{
//if goes in here, then image description is not null
NSURL *imageURL = [NSURL URLWithString:resolutionObj.imageDescription];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[resolutionImage setImage:[UIImage imageWithData:imageData]];
}
else
{
//if goes in here, then image description is NULL
//show empty image
[resolutionImage setImage:NULL];
}
}
You are missing a lot the way I see it. If your images are coming from a website then you should use a webView to display it. If you are planning to display images in UIImageView, you should first download them onto your device from the url that points to that image and save it in the sandbox. Then you store the path of this downloaded image in your sqlitedb. Now when you want to load this image you just access this image using the path that you have in your sqlitedb and render it. How do you think by just saving the imageName image013.png and not downloading the image itself will render the image for you in UIImageView. Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. For going and finding on the web directly, you need a UIWebView and not an UIImageView
If you are packaging your images as resources with your app binary, you can load them as shown below:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myimagefile" ofType:#"png"]];
[self.testView.imgView setImage:img];
where myimagefile is the name of your file and ofType is the type of image. .png or .jpg or whatever.
If you have anymore doubts. Please come back.