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)
}
Related
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
I am trying to create pdf from scrollview. The pdf is created successfully. But there is no padding on the top and bottom of the pdf file. i want the padding in each page of the pdf file.can anyone help me to resolve it? if the pdf file is consists on two or more page so parts are not visible as there is no padding
class CreatePDF {
func PDFWithScrollView(scrollview: UIScrollView) -> NSData {
let pageDimensions = scrollview.bounds
let pageSize = pageDimensions.size
let totalSize = scrollview.contentSize
let numberOfPagesThatFitHorizontally = Int(ceil(totalSize.width / pageSize.width))
// let numberOfPagesThatFitVertically = 1
let numberOfPagesThatFitVertically = Int(ceil(totalSize.height / pageSize.height))
let outputData = NSMutableData()
UIGraphicsBeginPDFContextToData(outputData, pageDimensions, nil)
let savedContentOffset = scrollview.contentOffset
let savedContentInset = scrollview.contentInset
scrollview.contentInset = UIEdgeInsets.zero
if let context = UIGraphicsGetCurrentContext()
{
for indexHorizontal in 0 ..< numberOfPagesThatFitHorizontally
{
for indexVertical in 0 ..< numberOfPagesThatFitVertically
{
UIGraphicsBeginPDFPage()
let offsetHorizontal = CGFloat(indexHorizontal) * pageSize.width
let offsetVertical = CGFloat(indexVertical) * pageSize.height
scrollview.contentOffset = CGPoint(x: offsetHorizontal, y: offsetVertical)
context.translateBy(x: -offsetHorizontal, y: -offsetVertical)
scrollview.layer.render(in: context)
}
}
}
UIGraphicsEndPDFContext()
scrollview.contentInset = savedContentInset
scrollview.contentOffset = savedContentOffset
return outputData
}
}
let snapshotter = CreatePDF()
let data = snapshotter.PDFWithScrollView(scrollview: scrollView)
i add SCNNode and append this object a gif
and I want to keep this object fixed at the bottom of the page .I want to keep this object fixed at the bottom of the page.
And its width should be in accordance with the width of the phone
guard let pointOfView = self.pointOfView else { return }
let gifImage = UIImage.gifImageWithName(fileUrl)
let height = (gifImage?.size.height)! * gifImage!.scale
let width = (gifImage?.size.width)! * gifImage!.scale
let ratio = width/height
let newHeight = ratio*height
let newWidth = self.frame.width
DispatchQueue.global(qos: .background).async {
let gifNode = SCNNode()
let gifPlane = SCNPlane(width: newWidth, height: newHeight)
gifNode.geometry = gifPlane
gifNode.position = SCNVector3Make(0, 0, -1)
DispatchQueue.main.async {
let gifImageView = UIImageView(image: gifImage).layer
self.updatePosition(node: gifNode)
gifPlane.firstMaterial?.diffuse.contents = gifImageView
let orientation = SCNVector3(x: 0, y: 0, z: -1) // i have no idea about this
gifNode.position = orientation
pointOfView.addChildNode(gifNode)
}
}
but that's doesn't work well
can you help me ?
I have a problem with UIScrollView in my Xcode project. I am using it to show several pictures, but even when it has only one page it still scrolls horizontally. Maybe its something with setupPhotosInScrollView() or scrollViewDidScroll. I don't know how to fix this issue. It seems ok, but still getting this error. Please help!
Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
// Ad Title
eTitleLabel.text = "\(eObj[EVENTS_TITLE]!)"
// Get photos
let imageFile = eObj[EVENTS_IMAGE1] as? PFFile
imageFile?.getDataInBackground(block: { (data, error) in
if error == nil { if let imageData = data {
self.photosArray.append(UIImage(data: imageData)!)
self.setupPhotosInScrollView()
print("PHOTO 1")
}}})
DispatchQueue.main.async {
if self.eObj[EVENTS_IMAGE2] != nil {
self.pageControl.numberOfPages = 2
let imageFile = self.eObj[EVENTS_IMAGE2] as? PFFile
imageFile?.getDataInBackground(block: { (data, error) in
if error == nil { if let imageData = data {
self.photosArray.append(UIImage(data: imageData)!)
self.setupPhotosInScrollView()
print("PHOTO 2")
}}})
}
// ------------------------------------------------
// MARK: - SETUP PHOTOS IN SCROLLVIEW
// ------------------------------------------------
#objc func setupPhotosInScrollView() {
var X:CGFloat = 0
let Y:CGFloat = 0
let W:CGFloat = view.frame.size.width
let H:CGFloat = view.frame.size.height
let G:CGFloat = 0
var counter = 0
// Loop to create ImageViews
for i in 0..<photosArray.count {
counter = i
// Create a ImageView
let aImg = UIImageView(frame: CGRect(x: X, y: Y, width: W, height: H))
aImg.tag = i
aImg.contentMode = .scaleAspectFit
aImg.image = photosArray[i]
// Add ImageViews based on X
X += W + G
containerScrollView.addSubview(aImg)
} // ./ FOR loop
// Place Buttons into a ScrollView
containerScrollView.contentSize = CGSize(width: W * CGFloat(counter+2), height: H)
}
// ------------------------------------------------
// MARK: - CHANGE PAGE CONTROL PAGES ON SCROLL
// ------------------------------------------------
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageWidth = containerScrollView.frame.size.width
let page = Int(floor((containerScrollView.contentOffset.x * 2 + pageWidth) / (pageWidth * 2)))
pageControl.currentPage = page
}
Replace
containerScrollView.contentSize = CGSize(width: W * CGFloat(counter+2), height: H)
With
containerScrollView.contentSize = CGSize(width: W * CGFloat(photosArray.count), height: H)
as you set a width for contentSize that's not equal to number of added photos
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)
}