Get bytes from UIImage
By : user3134473
Date : March 29 2020, 07:55 AM
I wish this helpful for you UIImageJPEGRepresentation or UIImagePNGRepresentation will return NSData*'s that you can use to get the raw data See docs here
|
How do I use the NSString draw functionality to create a UIImage from text and combine this image with another UIImage
By : Mariana Tremurici
Date : March 29 2020, 07:55 AM
like below fixes the issue I have NSString draw functionality to create a UIImage from text and combine this image with another UIImage and also movable UIImage.now i want to combine this Two UIImage together into a single UIImage. code :
-(UIImage *)imageFromText:(NSString *)text
{
// set the font type and size
UIFont *font = [UIFont systemFontOfSize:20.0];
CGSize size = [text sizeWithFont:font];
// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
if (UIGraphicsBeginImageContextWithOptions != NULL)
UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
else
// iOS is < 4.0
UIGraphicsBeginImageContext(size);
// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger
//
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]);
// draw in context, you can use also drawInRect:withFont:
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
// transfer image
UIImage *Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return Image;
}
|
how to create an UIImage that represent a radial portion of an UIImage?
By : user3901259
Date : March 29 2020, 07:55 AM
With these it helps given an image below and a percentage say 25%. How can I generate another UIImage that represent 25% of the radial portion of the given image? Any help is greatly appreciated. code :
- (UIImage *)radialPortion:(float)percent image:(UIImage *)image
{
CGSize size = image.size;
CGRect rect = {CGPointZero,size};
UIGraphicsBeginImageContext(size);
CGFloat radius = MAX(size.width, size.height)/2;
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint center = CGPointMake(size.width/2, size.height/2);
[path moveToPoint:center];
[path addLineToPoint:CGPointMake(center.x, center.y-radius)];
[path addArcWithCenter:center radius:radius startAngle:-M_PI_2 endAngle:-M_PI_2+M_PI*2*percent clockwise:1];
[path closePath];
[path addClip];
[image drawInRect:rect];
return UIGraphicsGetImageFromCurrentImageContext();
}
|
How to create a UIImage from bytes
By : Bounta Btb
Date : March 29 2020, 07:55 AM
help you fix your problem I've a String with byte code of a Image, I want create a UIImage from it. , Try this. code :
let data : Data = Data(base64Encoded: codeBytes, options: .ignoreUnknownCharacters)!
cell.imgView.image = UIImage(data: data)
|
Create new UIImage from freehand drawn line over UIImage - Swift
By : user2197137
Date : March 29 2020, 07:55 AM
With these it helps If you want to create an image from a masked path, you can: Create UIBezierPath from user gesture Use that bezier path as the CAShapeLayer on the image view When the user gesture is done, remove that shape layer, but create new shape layer and use it as a mask Create image from that masked image view: Use UIGraphicsBeginImageContextWithOptions to begin the image rendering process; Use drawHierarchy to render whatever should be captured in the image; Use UIGraphicsGetImageFromCurrentImageContext to capture the image from whatever you rendered, and Use UIGraphicsEndImageContext to indicate that the image rendering process is done. code :
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
private var path: UIBezierPath?
private var strokeLayer: CAShapeLayer?
@IBAction func didHandlePan(_ gesture: UIPanGestureRecognizer) {
let location = gesture.location(in: imageView)
switch gesture.state {
case .began:
path = UIBezierPath()
path?.move(to: location)
strokeLayer = CAShapeLayer()
imageView.layer.addSublayer(strokeLayer!)
strokeLayer?.strokeColor = #colorLiteral(red: 1, green: 0.1491314173, blue: 0, alpha: 1).cgColor
strokeLayer?.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
strokeLayer?.lineWidth = 5
strokeLayer?.path = path?.cgPath
case .changed:
path?.addLine(to: location)
strokeLayer?.path = path?.cgPath
case .cancelled, .ended:
// remove stroke from image view
strokeLayer?.removeFromSuperlayer()
strokeLayer = nil
// mask the image view
let mask = CAShapeLayer()
mask.fillColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1).cgColor
mask.strokeColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
mask.lineWidth = 0
mask.path = path?.cgPath
imageView.layer.mask = mask
// get cropped image
guard let image = imageView?.snapshot else { return }
imageView.layer.mask = nil
// perhaps use that image?
imageView.image = image
default: break
}
}
}
extension UIView {
var snapshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0)
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
|