while looping, Images are not adding to UIImageView - swift

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)
}

Related

How to use getPosition to get the last data entry position? Charts Pod

I'm trying to add an UIImage for the last data entry point but when I use getPosition the image was in the wrong place. Or there is other way to do it? I tried this answer but the image position was still off.
if dataPoint.count > 0 {
for i in 0..<dataPoint.count {
let value = ChartDataEntry(x: Double(i), y: dataPoint[i].running)
lineChartEntry.append(value)
}
}
let point = lineChartView.getPosition(entry: lineChartEntry[dataPoint.count - 1], axis: .left)
let image = UIImage(named: "heart")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: point, y: point, width:15, height: 15)
lineChartView.addSubview(imageView)
> po point (59.11698190789474, 28.672200520833343)
> - x : 59.11698190789474
> - y : 28.672200520833343
Did more research and testing I finally get it working.
In where you are setting the LineChartDataSet()
//data line setup
let line = LineChartDataSet(entries: lineChartEntry, label: "")
line.mode = .linear
line.colors = [color]
line.fillColor = .clear
line.drawFilledEnabled = true
line.drawCirclesEnabled = true
//remove old imageView when the new data comes in
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}
var colors = [UIColor]()
for i in 0..<lineChartEntry.count {
//only the last circle will show the image
if(i == lineChartEntry.count - 1) {
colors.append(color)
let point = lineChartView.getPosition(entry: lineChartEntry[lineChartEntry.count - 1], axis: .left) //get the x and y CGPoint
let image = UIImage(named: "heart")
let imageView = UIImageView(image: image!)
imageView.tag = (100)
if !point.x.isNaN && !point.y.isNaN {
imageView.frame = CGRect(x: point.x - 15, y: point.y - 15, width:30, height: 30) //set the frame in center
imageView.contentMode = .center
lineChartView.addSubview(imageView)
}
} else {
colors.append(UIColor.clear)
}
}
.
.
.
//other line data configurations

Multiple subviews being added to UIScrollview

On my users profile page, there is a scrollview users slide across to see more photos. Everything works with adding the photos but the problem is that each image added to the scrollviews subview, is added twice. This is the code I am using to populate the scrollview. I currently have an Imageview embed inside the scrollview on storyboard and use that as the default if user has no photos. The at the bottom should show the bug. I have tried using a combination of clipstobounds, scaleaspectfit, and setting background color to clear but nothing stops from showing an additional image in the background. Any ideas would be appreciated. On a side note, the bug is only noticed on the iphone 8 plus
var pages: Int = 1
var numPhotos: CGFloat = 1
var position: CGFloat = 1
let scrollWidth: CGFloat = profileImage.frame.width
let scrollHeight: CGFloat = profileImage.frame.height
let scrollY: CGFloat = profileImage.frame.origin.y
if let profileImg = user.profileImage {
profileResource = ImageResource(downloadURL: URL(string: profileImg)!, cacheKey: profileImg)
profileImage.kf.setImage(with: profileResource)
} else {
profileImage.image = #imageLiteral(resourceName: "requests_icon")
}
nameLbl.text = user.fullName
influenceLbl.text = "\(user.score)"
followersLbl.text = "\(user.followers)"
followingLbl.text = "\(user.following)"
if let image1 = user.photo1 {
let imageOne = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageOne.contentMode = .scaleAspectFit
photo1Resource = ImageResource(downloadURL: URL(string: image1)!, cacheKey: image1)
imageOne.kf.setImage(with: photo1Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageOne)
} else {
}
if let image2 = user.photo2 {
let imageTwo = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageTwo.contentMode = .scaleAspectFit
photo2Resource = ImageResource(downloadURL: URL(string: image2)!, cacheKey: image2)
imageTwo.kf.setImage(with: photo2Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageTwo)
} else {
}
if let image3 = user.photo3 {
let imageThree = UIImageView(frame: CGRect(x: scrollWidth * position, y: scrollY, width: scrollWidth, height: scrollHeight))
imageThree.contentMode = .scaleAspectFit
photo3Resource = ImageResource(downloadURL: URL(string: image3)!, cacheKey: image3)
imageThree.kf.setImage(with: photo3Resource)
pages = pages + 1
numPhotos = numPhotos + 1
position = position + 1
scrollView.addSubview(imageThree)
} else {
}
scrollView.contentSize = CGSize(width: profileImage.frame.width * numPhotos, height: scrollHeight)
scrollView.delegate = self
scrollView.contentMode = .scaleAspectFit
scrollView.clipsToBounds = true
pageControl.numberOfPages = pages
pageControl.currentPage = 0
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth) + 1
self.pageControl.currentPage = Int(currentPage)
}

Cropping a captured still image

I want just a specific part of my captured image to be cropped in a circular shape right before it's viewed.
Here is the code for capturing still images:
#IBAction func takePhoto(sender: UIButton) {
self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo)) { (buffer:CMSampleBuffer!, error:NSError!) -> Void in
var image = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
var data_image = UIImage(data: image)
self.previewImage.image = data_image
}
}
Thank you
if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer) {
if let image = UIImage(data: imageData) {
let square = image.size.width < image.size.height ? CGSize(width: image.size.width, height: image.size.width) : CGSize(width: image.size.height, height: image.size.height)
let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.image = image
imageView.layer.cornerRadius = square.width/2
imageView.layer.masksToBounds = true
UIGraphicsBeginImageContext(imageView.bounds.size)
imageView.layer.renderInContext(UIGraphicsGetCurrentContext())
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.previewImage.image = result
}
}

Rotate NSImageView in Swift

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.

Delete ImageView From SubScrollView In Swift

I am having main ScrollView with pages Enable..Under that I have Subscrollviews and ImageView to display images..I want to Delete Selected image from main scrollview..I don't have any idea to how to get that selected imageView from scrollview and remove it...Here is my code...
func createScroll()
{
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var index = NSUserDefaults.standardUserDefaults().objectForKey("Index") as Int
var scroll = UIScrollView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
var page = UIPageControl(frame: CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height))
page.numberOfPages = arrayImage.count
page.currentPage = 0
page.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
scroll.contentSize = CGSizeMake(scroll.frame.size.width * CGFloat(arrayImage.count - 1) , 0)
scroll.delegate = self
scroll.pagingEnabled = true
scroll.maximumZoomScale = 6
scroll.minimumZoomScale = 0.5
scroll.tag = Tags.Tag_MainScroll.rawValue
scroll.addSubview(page)
view.addSubview(scroll)
scroll.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
scroll.contentOffset = CGPointMake(scroll.frame.size.width * CGFloat(index - 1) , 0)
}
//MARK: Display Selected Image
func displayImage()
{
var mainScroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var xPosition : CGFloat = 0
for i in 1...22
{
var subScroll = UIScrollView(frame: CGRectMake(xPosition, 0, view.frame.size.width, view.frame.size.height))
subScroll.delegate = self
subScroll.maximumZoomScale = 6
subScroll.minimumZoomScale = 0.5
subScroll.tag = Tags.Tag_SubScroll.rawValue + i
mainScroll.addSubview(subScroll)
var imgToDisplay : UIImage = UIImage(named: "\(arrayImage[i])")! as UIImage
var imgView : UIImageView = UIImageView(frame: CGRectMake((subScroll.frame.size.width - 100 ) / 2,(subScroll.frame.size.height - 100) / 2, 100, 100))
subScroll.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height)
imgView.image = imgToDisplay
imgView.tag = Tags.Tag_imgView.rawValue
subScroll.addSubview(imgView)
xPosition += subScroll.frame.size.width
}
}
Here Is my Delete Function...
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
for img in scroll.subviews
{
imgView = img as? UIImageView
imgView?.removeFromSuperview()
}
}`
have u tryed this one..
let subViews = self.scrollView.subviews
for subview in subViews{
subview.removeFromSuperview()
}
HERE is the reference
as per my code this Delete Function is working for me
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var number : Int = Int(scroll.contentOffset.x / scroll.frame.size.width)
var page : UIPageControl = view.viewWithTag(Tags.Tag_page.rawValue) as UIPageControl
page.currentPage = number
println("\(page.currentPage)")
if var array : NSArray = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as? NSArray
{
arrayImage = array.mutableCopy() as NSMutableArray
}
page.removeFromSuperview()
number++
arrayImage.removeObjectAtIndex(number)
NSUserDefaults.standardUserDefaults() .setObject(arrayImage, forKey: "ImageArray")
println("\(arrayImage[number])")
println("\(arrayImage)")
NSUserDefaults.standardUserDefaults().synchronize()
displayImage()
}