How to manage UIImageView content mode?

Please try this code, Hope it will work for you. set UIImageView contentMode to UIViewContentModeScaleAspectFill as below : imageView.contentMode = UIViewContentModeScaleAspectFill; set autoresizingMask of UIImageView as below : imageView.autoresizingMask = ( UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth );

How to display a base64 image within a UIImageView?

You don’t have to encode it. Simply make a NSUrl, it knows the “data:”-url. NSURL *url = [NSURL URLWithString:base64String]; NSData *imageData = [NSData dataWithContentsOfURL:url]; UIImage *ret = [UIImage imageWithData:imageData]; As mentioned in the comments, you have to make sure that you prepend your data with data:image/png;base64, or else your base64 data is useless.

add UIImage in CALayer

This is a general answer for the sake of future viewers. It is based on the question title rather than the details of the original question. How to add a UIImage to a CALayer You can add an image to a view’s layer simply by using its contents property: myView.layer.contents = UIImage(named: “star”)?.cgImage Note that … Read more

Apple Interface Builder: adding subview to UIImageView

You cannot add a subview to UIImageView in interface builder for reasons only known to Apple! You are right in saying that you can addSubview programmatically, but then, the overhead of setting autoresizing masks and placements of subviews should all be handled in code, which is cumbersome. So there is an easy workaround. Instead of … Read more

Storing images locally on an iOS device

The simplest way is to save it in the app’s Documents directory and save the path with NSUserDefaults like so: NSData *imageData = UIImagePNGRepresentation(newImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@”%@.png”,@”cached”]]; NSLog(@”pre writing to file”); if (![imageData writeToFile:imagePath atomically:NO]) { NSLog(@”Failed to cache image data to … Read more

“Creating an image format with an unknown type is an error” with UIImagePickerController

Below mentioned code did solve the problem for me – func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { imagePost.image = image } else{ print(“Something went wrong”) } self.dismiss(animated: true, completion: nil) }

Center content of UIScrollView when smaller

I’ve got very simple solution! All you need is to update the center of your subview (imageview) while zooming in the ScrollViewDelegate. If zoomed image is smaller than scrollview then adjust subview.center else center is (0,0). – (void)scrollViewDidZoom:(UIScrollView *)scrollView { UIView *subView = [scrollView.subviews objectAtIndex:0]; CGFloat offsetX = MAX((scrollView.bounds.size.width – scrollView.contentSize.width) * 0.5, 0.0); CGFloat … Read more