I am trying to make thumbnail images and save to the document directory.
But the problem is that when I am trying to convert the thumbnail images to NSData. It returns nil.
Here is my code,
UIImage *thumbNailimage=[image thumbnailImage:40 transparentBorder:0.2 cornerRadius:0.2 interpolationQuality:1.0];
NSData *thumbNailimageData = UIImagePNGRepresentation(thumbNailimage);// Returns nil
[thumbNailimageData writeToFile:[DOCUMENTPATH stringByAppendingPathComponent:#"1.png"] atomically:NO];
So what is the problem I have also tried with UIImageJPEGRepresentation but it not works for me.
Thanks.
Try this:
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This creates a copy of the original UIImage. You can then call UIImagePNGRepresentation and it will work correctly.
Try this code,
-(void) createThumbnail
{
UIImage *originalImage = imgView2.image; // Give your original Image
CGSize destinationSize = CGSizeMake(25, 25); // Give your Desired thumbnail Size
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *thumbNailimageData = UIImagePNGRepresentation(newImage);
UIGraphicsEndImageContext();
[thumbNailimageData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:#"1.png"] atomically:NO];
}
Hope this Helps you,
happy Coding
To Swift programmers, Rickster's answer helped me a lot! UIImageJPEGRepresentation was crashing my app when selecting a certain image. I'm sharing my extension of UIImage (or Category in Objective-C term).
import UIKit
extension UIImage {
/**
Creates the UIImageJPEGRepresentation out of an UIImage
#return Data
*/
func generateJPEGRepresentation() -> Data {
let newImage = self.copyOriginalImage()
let newData = UIImageJPEGRepresentation(newImage, 0.75)
return newData!
}
/**
Copies Original Image which fixes the crash for extracting Data from UIImage
#return UIImage
*/
private func copyOriginalImage() -> UIImage {
UIGraphicsBeginImageContext(self.size);
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return newImage!
}
}
Related
I'm implementing a react-native component for iOS and need to return a UIImage.
What I have is return [UIImage imageNamed:#"myAsset"]; which is working but the image presented is way too small.
How can I load an asset image and make it bigger?
Another aspect is, that the impl returning the UIImage is invoked quite often for objects we draw onto the screen, but they are all the same, so scaling with every call is maybe not a good idea, but I have no idea how to make assets having a size. Last but not least, it's a PDF asset.
What I've tried is this ...
So I search for image scaling and came here:
NSURL *url = [NSBundle.mainBundle URLForResource:#"myAsset" withExtension:nil];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data scale:0.5];
return image;
but now url is nil and it's not working.
Then I found this:
UIImage *image = [UIImage imageNamed:#"myAsset"];
NSData *rawData = (__bridge NSData *) CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
return [UIImage imageWithData:rawData scale:0.5];
Which is somehow also not working at all.
Now I hope maybe you can help me and thank you in advance.
func resizeImage(image: UIImage, newWidth: CGFloat, newHeight: CGFloat) -> UIImage {
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight))
image.drawInRect(CGRectMake(0, 0, newWidth, newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
I want to scale down UIImage and use following method.
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
It returns correct dimensions but having larger image size(memory) than original image. How to optimize the method?
You can compress your image so its size will decrese
NSData* data = UIImageJPEGRepresentation(image, 0.8);
I am having problem with UIImage resizing, image masking is working fine but after applying mask UIImage is starched, the problem is with scaling as image is not scaled properly.
CCClippingNode *clippingNode = [[CCClippingNode alloc] initWithStencil:pMaskingFrame ];
pTobeMasked.scaleX = (float)pMaskingFrame.contentSize.width / (float)pTobeMasked.contentSize.width;
pTobeMasked.scaleY = (float)pMaskingFrame.contentSize.height / (float)pTobeMasked.contentSize.height;
clippingNode.alphaThreshold = 0;
[pContainerNode addChild:clippingNode];
pTobeMasked.position = ccp(pMaskingFrame.position.x, pMaskingFrame.position.y);
[clippingNode addChild:pTobeMasked];
One of my project I have used below function to resize an image;
/*
method parameters definition
image : original image to be resized
size : new size
*/
+ (UIImage*)resizeImage:(UIImage *)image size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//here is the scaled image which has been changed to the size specified
UIGraphicsEndImageContext();
return newImage;
}
This will work like a charm. It's similar to the already posted answer, but it has some more options:
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I have this code to resize my image into a thumbnail:
//MAKE THUMBNAIL
CGSize origImageSize = chosenImage.size;
//the rectangle of the thumbnail;
CGRect newRect = CGRectMake(0, 0, 70, 70);
//scaling ratio
float ratio = MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
CGRect projectRect;
projectRect.size.width= ratio*origImageSize.width;
projectRect.size.height=ratio*origImageSize.height;
//center the image
projectRect.origin.x= ((newRect.size.width-projectRect.size.width)/2);
projectRect.origin.y=((newRect.size.height-projectRect.size.height)/2);
[chosenImage drawInRect:projectRect];
//get the image from the image context
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
self.thumbnailView.image=smallImage;
UIImageWriteToSavedPhotosAlbum(smallImage, self, nil, nil); //20kb
UIGraphicsEndImageContext();
it saves the image as a jpg, is there anyway I can save it as a png instead? Thanks
Try converting your image's data to a PNG data representation then back into a PNG image, ex:
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *pngImageData = UIImagePNGRepresentation(smallImage);
UIImage *pngSmallImage = [UIImage imageWithData:pngImageData];
UIImageWriteToSavedPhotosAlbum(pngSmallImage, self, nil, nil); //20kb
UIGraphicsEndImageContext();
I've been reading resolved questions on how to programmatically take a screenshot but I can't seem to get what I've read to work in sprite kit. For instance:
This question How to take a screenshot programmatically
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:#"foo.png" atomically:YES];
UPDATE April 2011: for retina display, change the first line into this:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.window.bounds.size);
gave me this information, which I tested on my game but it didn't work because window was not recognized on an SKScene. I tried replacing it with scene but that didn't work. Any suggestions?
I also tried this:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Which I found in this question: ios Sprite Kit screengrab?
But it didn't seem to work either because it didn't recognize scale, or bounds in the second line.
This is the best solution to take a screen shoot in your Sprite-Kit
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
func screenShot() {
UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0)
self.view!.drawViewHierarchyInRect(self.view!.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
var Image = image //The image is stored in the variable Image
}
screenshot() //Calls the function and takes a screenshot
// Full Screen Shot function. Hope this will work well in spritekit.
func screenShot() -> UIImage {
UIGraphicsBeginImageContext(CGSizeMake(frame.size.width, frame.size.height))
var context:CGContextRef = UIGraphicsGetCurrentContext()
self.view?.drawViewHierarchyInRect(frame, afterScreenUpdates: true)
var screenShot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return screenShot
}
you can take screen shot for a any view.UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();