iOS: sharing a transparent UIImage w/ UIActivityViewController? - ios

So I am using UIActivityViewController to let users share or save an image they create with my app. I include a UIImage as one of the share items, it all works fine.
Except: it is possible to create this image with some transparent areas. And it looks to me like the built-in UIActivities all create JPEG representations of the UIImage, thus losing the transparency.
Is there a way to force it to use a PNG representation so as not to lose the alpha channel?

Without seeing some code it may be difficult to fully answer the question. Have you tried converting your UIImage to an NSData object using UIImagePNGRepresentation and sharing the NSdata object instead of the image itself? I would have put this in a comment rather than an answer, but I lack the rep.
Reference for UIImagePNGRepresentation:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation

Related

Modify UIImage RGB CMY colors

First of all I'm working with OCR and I have done a huge research about it, now I got to a conclusion of what exactly I need to do, working with OCR require the UIImage to be black and white converted. Now only this specifications worked for me but I needed to do it in iOS (Swift or Objective-c) code but honestly I couldn't find a good resource which helps me to modify the image RGB CMY in the image for a percentage like Adobe Photoshop does.
I really don't need to modify RBG only, its a combination which will generate a valid UIImage and works perfectly with OCR. Truly I don't have much experience in converting UIImage pixels.

Image size optimization for less data usage

I'm working on a Instagram-like app on iOS, and I'm wondering on how to optimize the file size of each picture so the users will use as least data as possible when fetching all those pictures from my backend. It seems like it will drain data if you download the file just as it is since high resolution pictures are around 1.5MB each. Is there a way to shrink the size of the picture while maintaining the quality of the picture as much as possible?
you can compress the image by saving it into json binary data.
OR simply core binary data.
OR elegantly Swift Realm - Core binary data.
Do you really want to customise image by you own! because there are lots of libraries already available for doing that.
Which are more effective and powerful.
like,
AFNetworking Api
Its is wonderful it could not only compressed images as per UIImageView current available size according to device resolution but also give you image cache flexibility.
Here is the link of Pod File and github
Just try It you will love it
You can compress a UIImage by converting it into NSData
UIImage *rainyImage =[UImage imageNamed:#"rainy.jpg"];
NSData *imgData= UIImageJPEGRepresentation(rainyImage,0.1 /*compressionQuality*/); // Second parameter of this function is the compression Quality
Now use this NSData object to save or convert it into UIImage
To convert it again into UIImage
UIImage *image = [UIImage imageWithData:imgData];
Hope this will resolve your issue.
Image compression will loose in the image quality.

Should I use UIImage or CGImage in a Swift iOS App?

All the code I can find revolves around loading images directly into visual controls.
However, I have my own cache system (converting a project from another language) and so I as efficient as possible want the following:
Load jpg/png images - probably into a bitmap / cgimage. (his can either be from the file system or from images downloaded online)
Possibly save image back as a compressed/resized png/jpg file
Supply an image reference for a visual control
I am new to swift and ios platform, but as far as I can tell, cgimage is as close as it gets? However, there does not appear to be a way to load an image from he file system when using cgimage... But i have found people discussing ways for e.g. UIImage, so I am now doubting my initial impression ha cgimage was the best match for my needs.
It is easy to get confused between UIImage, CGImage and CIImage. The difference is following:
UIImage: UIImage object is a high-level way to display image data. You can create images from files, from Quartz image objects, or from raw image data you receive. They are immutable and must specify an image’s properties at initialization time. This also means that these image objects are safe to use from any thread.
Typically you can take NSData object containing a PNG or JPEG representation image and convert it to a UIImage.
CGImage: A CGImage can only represent bitmaps. Operations in CoreGraphics, such as blend modes and masking require CGImageRefs. If you need to access and change the actual bitmap data, you can use CGImage. It can also be converted to NSBitmapImageReps.
CIImage: A CIImage is an immutable object that represents an image. It is not an image. It only has the image data associated with it. It has all the information necessary to produce an image.
You typically use CIImage objects in conjunction with other Core Image classes such as CIFilter, CIContext, CIColor, and CIVector. You can create CIImage objects with data supplied from variety of sources such as Quartz 2D images, Core Videos image, etc.
It is required to use the various GPU optimized Core Image filters. They can also be converted to NSBitmapImageReps. It can be based on the CPU or the GPU.
In conclusion, UIImage is what you are looking for. Reasons are:
You can get image from device memory and assign it to UIImage
You can get image from URL and assign it to UIImage
You can write UIImage in your desired format to device memory
You can resize image assigned to UIImage
Once you have assigned an image to UIImage, you can use that instance in controls directly. e.g. setting background of a button, setting as image for UIImageView
Would have added code samples but all these are basic questions which have been already answered on Stackoverflow, so there is no point. Not to mention adding code will make this unnecessarily large.
Credit for summarizing differences: Randall Leung
You can load your image easily into an UIImage object...
NSData *data = [NSData dataWith...];
UIImage *image = [UIImage imageWithData:data];
If you then want to show it in a view, you can use an UIImageView:
UIImageView *imageView = [[UIImageView alloc] init]; // or whatever
...
imageView.image = image;
See more in UIImage documentation.
Per the documentation at: https://developer.apple.com/documentation/uikit/uiimage
let uiImage = uiImageView.image
let cgImage = uiImage.cgImage

Modifying channels in an image

How does one modify the colour and alpha channels in an image?
I am able to apply a colour to an image using CGContextSetRGBFillColor which creates a weird effect.
How can a colour channel be modified?
You want to create a bitmap (CGBitmapContextCreate) and draw your image to it. That will give you access to the image pixels. To change image data, you can then modify the bitmap bytes directly.
If your use case is simple enough, you can use different techniques, e.g. multiple images with transparency, masks...
Also, check out Core Image Filters (CIFilter) available on iOS. One of them may be suitable for you use cases.

Converting SVG into arbitrarily sized image on iOS

I have several SVG images that I would like to use in an iOS application, and, in short, I would like to turn the SVG images into UIImages (or CGImages).
My goal is that I should be able to load the images from the .svg files at an arbitrary size (assuming correct W/H ratio) and store them as UIImages or CGImages without any loss of image quality. (Note this all has to happen at runtime, pre-converting the images to various sized .png files and putting them in the App bundle isn't a viable option.)
Is this possible, and if so, how could I go about doing this? I have a good working knowledge of Core Graphics but I have never worked with vector graphics before.
SVGKit could help you - this library renders SVGs onto CALayer instances; from that, you can easily composite the image to a bitmap and make a CGImage of it.
I would not recommend converting a vectored document to a bitmap in most instances, but if you must SVGgh has a class SVGRenderer which has a method -(UIImage*)asImageWithSize:(CGSize)maximumSize andScale:(CGFloat)scale which will create a UIImage.
This is assuming SVGgh renders your SVGs properly, it doesn't support image effects, for instance.
#import <SVGgh/SVGgh.h>
-(UIImage) imageFromSVGURL:(NSURL*)svgURL withSize:(CGSize)maximumSize andScale:(CGFloat)scale
{
SVGRenderer* renderer = [[SVGRenderer alloc] initWithContentsOfURL:svgURL];
return [renderer asImageWithSize:maximumSize andScale:scale];
}

Resources