Here i m trying to save image in gallery. i done this but now i want to save captured image in date wise folder in gallery.
- (IBAction)saveImage:(id)sender {
UIGraphicsBeginImageContextWithOptions(drawingSlate.bounds.size, drawingSlate.opaque, 0.0);
[drawingSlate.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
here the image is save in photos, but i want to save with proper name and datewise folder.
Thanks
Related
I want save image to camera roll in apple Watch
this is my code:
UIImage *image = [UIImage imageNamed:#"w0-1.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
can you help me please??
You cannot save image to saved photo album. You can only save image to your application folder.
I know there has to be a way to take one base image (in my case a transparent 4096x4096 base layer) and adding several dozen (up to 100) very small (100x100) images to it in different locations. BUT I cannot figure out how to do it: Assume for the following that imageArray has many elements, each containing a NSDictionary with the following structure:
#{
#"imagename":imagename,
#"x":xYDict[#"x"],
#"y":xYDict[#"y"]
}
Now, here is the code (that is not working. all I have is a transparent screen...and the performance is STILL horrible...about 1-2 seconds per "image" in the loop...and massive memory use.
NSString *PNGHeatMapPath = [[myGizmoClass dataDir] stringByAppendingPathComponent:#"HeatMap.png"];
#autoreleasepool
{
CGSize theFinalImageSize = CGSizeMake(4096, 4096);
NSLog(#"populateHeatMapJSON: creating blank image");
UIGraphicsBeginImageContextWithOptions(theFinalImageSize, NO, 0.0);
UIImage *theFinalImage = UIGraphicsGetImageFromCurrentImageContext();
for (NSDictionary *theDict in imageArray)
{
NSLog(#"populateHeatMapJSON: adding: %#",theDict[#"imagename"]);
UIImage *image = [UIImage imageNamed:theDict[#"imagename"]];
[image drawInRect:CGRectMake([theDict[#"x"] doubleValue],[theDict[#"y"] doubleValue],image.size.width,image.size.height)];
}
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(theFinalImage);
theFinalImage = nil;
[imageData writeToFile:PNGHeatMapPath atomically:YES];
NSLog(#"populateHeatMapJSON: PNGHeatMapPath = %#",PNGHeatMapPath);
}
I KNOW I am misunderstanding UIGraphics contexts...can someone help me out? I am trying to superimpose a bunch of UIImages on the blank transparent canvas at certain x,y locations, get the final image and save it.
PS: This is also .. if the simulator is anything to go by ... leaking like heck. It goes up to 2 or 3 gig...
Thanks in advance.
EDIT:
I did find this and tried it but it requires me to rewrite the entire image, copy that image to a new one and add to that new one. Was hoping for a "base image", "add this image", "add that image", "add the other image" and get the new base image without all the copying:
Is this as good as it gets?
+(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake( point.x, point.y, fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
You are getting the image UIGraphicsGetImageFromCurrentImageContext() before you have actually drawn anything. Move this call to after your loop and before the UIGraphicsEndImageContext()
Your drawing actually occurs on the line
[image drawInRect:CGRectMake([theDict[#"x"] doubleValue],[theDict[#"y"] doubleValue],image.size.width,image.size.height)];
the function UIGraphicsGetImageFromCurrentImageContext() gives you the image of the current contents of the context so you need to ensure that you have done all your drawing before you grab the image.
Xcode 5, iOS 7
I am loading an image into a UIImage, and then copy it into another UIImage with a Mirror transform, i.e.:
self.imageB.image=[UIImage imageWithCGImage:[self.imageA.image CGImage] scale:1.0 orientation:UIImageOrientationUpMirrored];
Next, I'm trying to combine the two images into one when saving (originalImage refers to the loaded image prior to copying/transforming into imageA and imageB):
newSize=CGSizeMake(originalImage.size.width*2,originalImage.size.height);
UIGraphicsBeginImageContext(newSize);
UIImage *leftImage=self.imageA.image;
UIImage *rightImage=self.imageB.image;
[self.imageA.image drawInRect:CGRectMake(0,0,newSize.width/2,newSize.height)];
[self.imageB.image drawInRect:CGRectMake(newSize.width/2,0,newSize.width/2,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
This works and gives me a single saved mirrored image.
However, I'm trying to do this using only a sub-region of mageA and imageB,
and the result is that the portion from imageB loses it's Mirrored transformation.
I end up with both sides of the final having the same orientation!(Note: visRatio is the percentage of the image I want to keep)
newSize=CGSizeMake(originalImage.size.width*2,originalImage.size.height);
UIGraphicsBeginImageContext(newSize);
UIImage *leftImage=self.imageA.image;
UIImage *rightImage=self.imageB.image;
CGRect clippedRectA = CGRectMake(0,0,originalImage.size.width*visRatio,originalImage.size.height);
CGImageRef imageRefA = CGImageCreateWithImageInRect([self.imageA.image CGImage], clippedRectA);
UIImage *leftImageA = [UIImage imageWithCGImage:imageRefA];
[leftImageA drawInRect:CGRectMake(0,0,newSize.width/2,newSize.height)];
CGRect clippedRectB = CGRectMake(originalImage.size.width-(originalImage.size.width*visRatio),0,originalImage.size.width*visRatio,originalImage.size.height);
CGImageRef imageRefB = CGImageCreateWithImageInRect([self.imageB.image CGImage], clippedRectB);
UIImage *rightImageB = [UIImage imageWithCGImage:imageRefB];
[rightImageB drawInRect:CGRectMake(newSize.width/2,0,newSize.width/2,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
It's as though "CGImageCreateWithImageInRect" copies the image from the original, not the transformed, data.
How can I accomplish this without losing the mirrored transformations of imageB ?
I figured it out - this will use the original, full resolution data opened into originalImage and saved a clipped, transformed version (I left the mirroring out of this post for simplicity):
//capture the transformed version
CGSize tmpSize=CGSizeMake(originalImage.size.width, originalImage.size.height);
UIGraphicsBeginImageContext(tmpSize);
[self.imageA.image drawInRect:CGRectMake(0,0, tmpSize.width, tmpSize.height)];
UIImage *tmpImageA=UIGraphicsGetImageFromCurrentImageContext();
visRatio=0.5;
//clip it
clippedRectA=CGRectMake(0,0,roundf(originalImage.size.width*visRatio),originalImage.size.height);
imageRefA=CGImageCreateWithImageInRect([tmpImageA CGImage],clippedRectA);
leftImageA=[UIImage imageWithCGImage:imageRefA];
UIGraphicsEndImageContext();
savedImage=UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil);
I believe the key is to draw the image into the context, then use the context for cropping/saving, instead of the main image (imageA).
If you find a more efficient way, please post it!
Otherwise, I hope this helps someone else....
I want to save an image to camera roll with my own imagename in ios 5.I am using AV Foundation for accessing camera features.Please help me with possible solutions.
// Image to save
UIImage *img = [UIImage imageNamed:#"ImageName.png"];
// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(img, self,
#selector(image:didFinishSavingWithError:contextInfo:), nil);
Hope this helps..
In my main ViewController, I generate an image from UIGraphicsGetImageFromCurrentImageContext(). The image is assigned to a UIImage, and I've tested it by placing in a UIImageView in the main view, so I can see it. Works just fine. However when I tap a button I've assigned to save it, I get the error: -3304 "Failed to encode image for saved photos."
screenshot code:
CGSize mySize = CGSizeMake(530, 350);
UIGraphicsBeginImageContext(mySize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -444, -313);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// self.screenshot is a UIImage declared in .h
self.screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Save code:
UIImageWriteToSavedPhotosAlbum(screenshot, self, #selector(imageWasSavedSuccessfully:didSaveWithError:contextInfo:), NULL);
Not sure if I'm not meeting the requirements for camera roll images, or if that method can only be used in conjunction with the UIPickerController class. Thanks for your help.
For posterity, the issue was that the image to be saved was nil.
Try to save your image like this:
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil);
or maybe
UIImageWriteToSavedPhotosAlbum(self.screenshot, nil, nil, nil);
or maybe even
UIImageWriteToSavedPhotosAlbum(screenshot, self, nil, nil);