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!
Related
In my project am trying to compressing the images (coming from the server).I used HJManager for compressing. In that they are compressing the uiimage to user mentioned size, thats cool but taking too much of app memory.For 30 images it is taking almost 400 mb of live bytes. and its getting crashing. If you want to see my code, i'll paste it. But i want to know the best way to compress(mbs to kbs) the image which am getting from the server without disturbing the UI.
Switch to SDWebImage. Much better caching library with smaller memory footprint than HJManager and more modern, it uses blocks with completion handler. Last but not least, it is actively maintained which means that even if there is an issue, the community patches it.
You can resize the image easily with a method like this:
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
I have made an iphone app in which i download the images via FTP & saved in document Directory. These Images are of high memory size (approx. 2-3 MB).
Then I display these images in UITableview with many rows & due to these Images the allocation memory is too big & that's why app crashes.
So, I need to reduce the memory size of these images.
How can I do this?
Thanks,
Make thumbnail of the images before you add them to the UITableView. In this way the size of the images would reduce drastically. Use the following code on the UIImages you are picking from the document directory .
UIImage *originalImage = ...;
CGSize destinationSize = ...;
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Also have a look at the following links :-
Thumbnail view of images
http://www.icab.de/blog/2010/10/01/scaling-images-and-creating-thumbnails-from-uiviews/
https://gist.github.com/djbriane/160791
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!
I have a category (very popular code found on web) to UIImage to do various image manipulation.
- (UIImage *)imageScaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
One aspect that I am making heavy use of is scaling an image down. My app can take quite large images and scales them down to a "working" size. However, there are still times when the app crashes due to memory. This is because the category creates a new scaled image from the original. Therefore, the original HUGE image is still resident while the new smaller (but still big) image is created.
So, my question is, is there a way to load this large original image and rescale it in place? That is, rescale the original without creating a new image, and not allocing more memory?
Yes, and there is even a complete working Apple sample project that does this for you.
As far as I know there is no limitation on what size image it can scale down. Of course though, the larger the image the more time consuming the process is.
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.