简体   繁体   中英

UIImage not in wanted location of UIImageView

Problem solved, s. comment on first answer...

I am using a plugin called Carlos to load images from the cache if they have already been fetched once. However, the images I load only appear in the first of my (3) pages (I have a scrollView with pagingEnabled set to true and each of these pages shall contain the userProfilePicture ). In addition to that, the image(s) appearing on the first page also has(-ve) no offsets. Combining those two informations one can assume that all (3 due to 3 pages) images are currently located on the first page with an offset of 0 and 0 for x and y respectively (see image below)(therefore I added '(s)' and '(-ve)' previously). How can I place the loaded image in the UIImageView? Does anyone have an idea how to solve this problem?

Code (Swift)

let userProfilePicture: UIImageView = {

    let statusBarHeight = UIApplication.shared.statusBarFrame.height

    let profileImageFrame = CGRect(x: statusBarHeight / 2, y: statusBarHeight, width: 50, height: 50)
    let profileImageView = UIImageView(frame: profileImageFrame)

    //set backgroundcolor (placeholder as sort of a place holder)
    profileImageView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)

    let imageUrl = NSURL(string: "https://like.to.myImageWith50pixels.jpg")!

    let cache = MemoryCacheLevel().compose(DiskCacheLevel()).compose(NetworkFetcher())

    cache.get((imageUrl as NetworkFetcher.KeyType) as NetworkFetcher.KeyType)
        .onSuccess { value in
            let profileImage = UIImage(data: value as Data)


            profileImageView.image = profileImage
        }
        .onFailure { error in
            print("An error occurred :( \(error)")
    }

    profileImageView.translatesAutoresizingMaskIntoConstraints = false

    //round picture
    profileImageView.layer.borderWidth = 1
    profileImageView.layer.masksToBounds = false
    profileImageView.layer.borderColor = UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 0.75).cgColor
    profileImageView.layer.cornerRadius = profileImageView.frame.height/2
    profileImageView.clipsToBounds = true

    return profileImageView

}()

所有三个图像可能都在彼此下方

one more solution I made it. Make a singleton class with a NSCache parameter. using GCD fetch images and save image data for location key.

for every time you want image. use singleton class NSCache parameter to fetch and image from web or local cache which every is available first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM