I using playground to rotate the following image.
let imagePath = pathToFileInSharedSubfolder("gauge.png")
let image = NSImage(byReferencingFile: imagePath)!
let imageView = NSImageView()
imageView.image = image
//Frames
let imageframe = imageView.frame
let origin = CGPointMake(imageframe.width/2, 0)
imageView.setFrameOrigin(origin)
imageView.rotateByAngle(35)
imageView.setNeedsDisplay()
let frame = CGRectMake(150, 0, 51, 126)
imageView.frame = frame
view.addSubview(imageView)
let refImageView = NSImageView()
refImageView.frame = frame
refImageView.image = image
view.addSubview(refImageView)
The image is not rotation about my origin point. See image below?
How does one change the image view origin? Thanks.
Related
Hey I create a textview and I can add images in this textview.This image's width equal to textview's width. But I want to give a maximum height for this ImageView and I want to show the image like content mode scale aspect fit but it shows stretched(compressed aspect fill) how can I solve this situation ? Code like below
let image = UIImageView()
image.contentMode = .scaleAspectFit
let imageAttachment = NSTextAttachment()
let newImageWidth = self.textView.bounds.width
let newImageHeight = 200
imageAttachment.bounds = CGRect(x: 0, y: 0, width: Int(newImageWidth), height: newImageHeight)
imageAttachment.image = image.image
This is how you would calculate the new height for an aspectFit ratio:
// don't use "image" ... that's confusing
let imageView = UIImageView()
// assuming you set the image here
imageView.image = UIImage(named: "myImage")
guard let imgSize = imageView.image?.size else {
// this will happen if you haven't set the image of the imageView
fatalError("Could not get size of image!")
}
let imageAttachment = NSTextAttachment()
let newWidth = self.textView.bounds.width
// get the scale of the difference in width
let scale = newWidth / imgSize.width
// multiply image height by scale to get aspectFit height
let newHeight = imgSize.height * scale
imageAttachment.bounds = CGRect(x: 0, y: 0, width: newWidth, height: newHeight)
imageAttachment.image = imageView.image
I would very much appreciate the help :), and also is there any way to make the underline change colors when entering a username or password?
Try adding below code:
let iconWidth = 20;
let iconHeight = 20;
//Define the imageView
let imageView = UIImageView();
let imageEmail = UIImage(named: "xyz.png");
imageView.image = imageEmail;
// set frame on image before adding it to the uitextfield
imageView.frame = CGRect(x: 5, y: 5, width: iconWidth, height: iconHeight)
textField.leftViewMode = UITextFieldViewMode.Always
textField.leftView = imageView
You can use UIImageView and this too as a subview inside the UITextField
I am making an app that has a UIWebView along with a button on a single view controller. When the button is clicked, an image (of the UIWebView) is captured using UIGraphicsContext.
This part works great! But when the button is clicked, after capturing the image, it displays the image as a subview on the same view, and I have been trying to use an ImageCropper Library that draws a CGRect in another subview over the UIImageView on the screen with a submit button. The rectangle itself can be resized (dragging the corners/edges) and moved around the view.
When the submit button is clicked, another subview is displayed in the top left hand portion of screen and display the image that was cropped (after clicking submit button) The idea is to only capture what is inside the rectangle. I am able to get the code working but the image captured is of the same image but not a section that is inside the CGRect.
I have 3 images that show how it works and shows the image that is cropped incorrectly.enter image description here . Shot 1 . Shot 2
Shot 3. I believe my problems lies within the size of image captured and the size of the image with the crop rect are not equal and that is why it is distorting it.
Does anyone know what might be the cause? Sorry for the long winded question but any help would be greatly appreciated!
Here is my code below:
ViewController.swift:
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
#IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
imageView.isHidden = true
let aString = URL(string: "https://www.kshuntfishcamp.com/home.page")
webView.loadRequest(URLRequest(url: aString!))
super.viewDidLoad()
}
#IBAction func takePhotoPressed(_ sender: UIButton) {
UIGraphicsBeginImageContextWithOptions(webView.bounds.size, false, 0.0)
if let aContext = UIGraphicsGetCurrentContext(){
webView.layer.render(in: aContext)
}
let capturedImage:UIImage? = UIGraphicsGetImageFromCurrentImageContext()
imageView = UIImageView(frame: CGRect(x: 22, y: 123, width: 330, height: 330))
let image = capturedImage
imageView.image = image
imageView.contentMode = UIViewContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.isHidden = true
webView.isHidden = true
let editView = EditImageView(frame: self.view.frame)
let image2 = capturedImage!
editView.initWithImage(image: image2)
let croppedImage = editView.getCroppedImage()
self.view.addSubview(editView)
self.view.backgroundColor = UIColor.clear
UIImageWriteToSavedPhotosAlbum(croppedImage, nil, nil, nil)
}
EditImageView.swift - source (https://github.com/Thanatos-L/LyEditImageView)-only including parts that seem relevant to solving the problem
func initWithImage(image:UIImage){
imageView = UIImageView(frame: CGRect(x: 22, y: 123, width: 330, height: 330))
imageView.tag = IMAGE_VIEW_TAG;
self.addSubview(self.imageView)
imageView.isUserInteractionEnabled = true;
imageView.image = image
imageView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
let frame = AVMakeRect(aspectRatio: imageView.frame.size, insideRect: self.frame);
imageView.frame = frame
originImageViewFrame = frame
NSLog("initWithImage %#", NSStringFromCGRect(originImageViewFrame))
imageZoomScale = 1.0
commitInit()
}
private func cropImage() {
let rect = self.convert(cropView.frame, to: imageView)
let imageSize = imageView.image?.size
let ratio = originImageViewFrame.size.width / (imageSize?.width)!
let zoomedRect = CGRect(x: rect.origin.x / ratio, y: rect.origin.y / ratio, width: rect.size.width / ratio, height: rect.size.height / ratio)
let croppedImage = cropImage(image: imageView.image!, toRect: zoomedRect)
var view: UIImageView? = self.viewWithTag(1301) as? UIImageView
if view == nil {
view = UIImageView()
}
view?.frame = CGRect(x: 0, y: 0, width: croppedImage.size.width , height: croppedImage.size.height)
view?.image = croppedImage
view?.tag = 1301
self.addSubview(view!)
}
I am trying to scrolling image automatically in swift. I have tried below. I am trying to scroll through swift coding, instead of StoryBoard. It is scrolling well. But, Images are not adding to the image view. But, UIImageView bgcolour has been changed to green. My coding is below.
Kindly Guide me.
var str_1 : String = "one.jpg"
var str_2 : String = "two.jpg"
let img_1 : UIImage = UIImage(named: str_1)!
let img_2 : UIImage = UIImage(named: str_2)!
img_arr.addObject(img_1)
img_arr.addObject(img_2)
scroll_view = UIScrollView(frame: CGRectMake(0, 200, 320, 130))
for(var y : Int = 0; y < img_arr.count; y++)
{
//scr_img_vw = UIImageView(image: img_1)
//scr_img_vw.addSubview(UIImageView(image: img_arr.objectAtIndex(y) as UIImage))
scr_img_vw = UIImageView(image: img_arr.objectAtIndex(y) as UIImage)
//scr_img_vw.image = UIImage(named: img_arr.objectAtIndex(y) as NSString)
scr_img_vw = UIImageView(frame: CGRectMake(320 * CGFloat(y), 0, 320, 130))
scr_img_vw.backgroundColor = UIColor.greenColor()
self.scroll_view.addSubview(scr_img_vw)
}
self.view.addSubview(scroll_view)
scroll_view.contentSize = CGSizeMake(scroll_view.frame.size.width * CGFloat(img_arr.count), 130)
scroll_view.scrollEnabled = true
scroll_view.pagingEnabled = true
scroll_view.bounces = true
You are allocating image twice. Change the fore loop as follows:
for(var y : Int = 0; y < img_arr.count; y++)
{
scr_img_vw = UIImageView(image: img_arr.objectAtIndex(y) as UIImage)
scr_img_vw.frame = CGRectMake(320 * CGFloat(y), 0, 320, 130))
scr_img_vw.backgroundColor = UIColor.greenColor()
self.scroll_view.addSubview(scr_img_vw)
}
The better swift way would be like this:
let img1 = UIImage(named: "one.jpg")!
let img2 = UIImage(named: "two.jpg")!
var imageArray = [UIImage]()
imageArray.append(img1)
imageArray.append(img2)
for (index, image) in enumerate(imageArray) {
var imageView = UIImageView(image: image)
imageView.frame = CGRectMake(320 * CGFloat(index), 0, 320, 130))
self.scrollView.addSubview(imageView)
}
I want to grab a subimage from a UIImage. I've looked around for a similar question, to no avail.
I know the range of pixels I want to grab - how can I return this subimage, from an existing image?
This should help: http://iphonedevelopment.blogspot.com/2010/11/drawing-part-of-uiimage.html
This code snippet is creating a category of UIImage but the code should be easily modified to work without it being a category.
A shorter way of doing the same thing is the following:
CGRect fromRect = CGRectMake(0, 0, 320, 480); // or whatever rectangle
CGImageRef drawImage = CGImageCreateWithImageInRect(image.CGImage, fromRect);
UIImage *newImage = [UIImage imageWithCGImage:drawImage];
CGImageRelease(drawImage);
Hope this helps!
Updated #donkim answer for swift 3:
let fromRect=CGRect(x:0,y:0,width:320,height:480)
let drawImage = image.cgImage!.cropping(to: fromRect)
let bimage = UIImage(cgImage: drawImage!)
In Swift 4, taking into account screen scale (otherwise your new image will be too large):
let img = UIImage(named: "existingImage")!
let scale = UIScreen.main.scale
let dy: CGFloat = 6 * scale // say you want 6pt from bottom
let area = CGRect(x: 0, y: img.size.height * scale - dy, width: img.size.width * scale, height: dy)
let crop = img.cgImage!.cropping(to: area)!
let subImage = UIImage(cgImage: crop, scale: scale, orientation:.up)