How do I create a png image from text in iOS?
How to save a view as an image:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It's covered in one of the first lectures in this term of Stanford's free iPhone programming course. There's a video and PDF notes.
Basically, you can create a UIImage and use its graphics context to draw into it, then save a PNG representation of the image.
Related
We need to create Thumbnail images for various image formats - > i.e.JPEG/PNG/16 colour, 256 colour and 24 bit Bitmap and store them to the local disk. These images will be used later for preview. The link provided supports for BMP,
Objective C - save UIImage as a BMP file
can you pl. clarify if that approach can be followed for GIF/TIFF formats also.
Currently the below mentioned approach is followed for creating thumbnail images.
Step 1 : Resize images to thumbnail size
UIGraphicsBeginImageContextWithOptions(resize, NO, 0.0);
[oldImage drawInRect:CGRectMake(0, 0, resize.width, resize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Step2: Save thumbnail image to local disk
NSData *fileData = UIImagePNGRepresentation(image);
NSString* temp = [NSString stringWithFormat:#"%#/%#",temp,name];
[[NSFileManager defaultManager]createFileAtPath:temp
contents:fileData attributes:nil];
For PNG and JPEG formats, the methods available are
i) UIImagePNGRepresentation();
ii) UIJPEGRepresentation()
Pl. let me know how other image formats need to be handled, since API supports only JPEG and PNG format. Apart from the above approach, if there are different ways to create thumbnail images pl. let me know. Any input/suggestion will be of help
I'm uploading an image taken on the iPhone(Converted to NSData) via FTP on an iOS application using https://github.com/lloydsargent/BlackRaccoon.
dataImage = UIImageJPEGRepresentation(image, 0.5);
The problem is that the image size is about 40mb and every time the app finish uploading, Xcode Crashes. How can I make the image smaller?
This method will allow you to resize an image in Objective-C programmatically.
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, YES, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Basically I recommend you resize the image and then upload it if the issue is the API you are using. That being said, using the NSURLConnection class methods, I have been able to upload files larger than 40mb. You can check out a post I just made out about how to use those methods to upload files with a PHP back-end here:
Strange issue while posting image from iPhone
Make sure to mark my answer as correct if it helps!
I want to create an image from an UIImageView and couple of UIViews attached on it.
The image will look exactly as the screenshot, but if I just take the screenshot, the image is created exactly the same size of screen. Now, if I create the image with bigger dimension, the screenshot is being distorted, though the UIImageView image is a really high resolution image.
So, I think this problem will be solved, if I create an image with the components other than just saving the screenshot.
I think this will do it.
(it doesn't matter how many views you have)
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"foo.png" atomically:YES];
I want to process a subregion of a UIImage in an iOS app. Following this question, I now have a routine to extract the region in question as a UIImage that I can now manipulate. Is there a similarly convenient method for placing the region back into the original image? The alternative I'm considering is a bytewise copy, which seems extremely low-level to me.
You could draw the two images on top of each other, and then combine them to one image.
Assuming you have the original image and the modified part:
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawAtPoint:CGPointMake(0, 0)];
[modifiedPart drawAtPoint:/* Upper left corner of the modified part */];
UIImage *combined = UIGraphicsGetImageFromCurrentImageContext();
Edit:
Forgot this line:
UIGraphicsEndImageContext();
How to take a screenshot programmatically?
You can use UIGraphicsBeginImageContext for this purpose.
For example :
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData*theImageData = UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too
[theImageData writeToFile:#"example.jpeg" atomically:YES];
Here
1. First i take the image context, i've given it as myView which is a webview, you can give whatever you wish for. This takes the image of webview that can be seen on the screen.
2 Using UIGraphicsGetImageFromCurrentImageContext() i convert the screenshot of my webview into an image.
3. By using UIGraphicsEndImageContext() i ask it end the context.
4. I'm saving the image into an NSData because i had to mail the screenshot. And keep it in NSData seemed a good option if it is used to send or save.
EDIT: To add it to the camera roll you need to write:
UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL); after UIGraphicsEndImageContext();
Have a look at this answer.It also takes care of retina display.
Actually to explain the process,
Choose a image context size (probably the layer size for which you need screen shot)
Render the layer which you want to take screenshot in the created context
Obtain the image from the context and you are done!