Issue when creating CIImage from file












0














I get a nil no matter what I try to load a CIImage directly from file. However I can load the file into a UIImage and convert that to CIImage.



So, this works:



        let testUIImage = UIImage(named: "image_0001.jpg")!
let testCIImage = CIImage(image: testUIImage) // OK


but this doesn't:



        let testCIImage = CIImage(contentsOf: URL(string: "image_0001.jpg")!)  // returns nil


What am I doing wrong? Is it the URL?










share|improve this question
























  • URL should convey the location of the image not the image name itself
    – iOSer
    Nov 21 '18 at 11:54










  • UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
    – Scriptable
    Nov 21 '18 at 11:54
















0














I get a nil no matter what I try to load a CIImage directly from file. However I can load the file into a UIImage and convert that to CIImage.



So, this works:



        let testUIImage = UIImage(named: "image_0001.jpg")!
let testCIImage = CIImage(image: testUIImage) // OK


but this doesn't:



        let testCIImage = CIImage(contentsOf: URL(string: "image_0001.jpg")!)  // returns nil


What am I doing wrong? Is it the URL?










share|improve this question
























  • URL should convey the location of the image not the image name itself
    – iOSer
    Nov 21 '18 at 11:54










  • UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
    – Scriptable
    Nov 21 '18 at 11:54














0












0








0







I get a nil no matter what I try to load a CIImage directly from file. However I can load the file into a UIImage and convert that to CIImage.



So, this works:



        let testUIImage = UIImage(named: "image_0001.jpg")!
let testCIImage = CIImage(image: testUIImage) // OK


but this doesn't:



        let testCIImage = CIImage(contentsOf: URL(string: "image_0001.jpg")!)  // returns nil


What am I doing wrong? Is it the URL?










share|improve this question















I get a nil no matter what I try to load a CIImage directly from file. However I can load the file into a UIImage and convert that to CIImage.



So, this works:



        let testUIImage = UIImage(named: "image_0001.jpg")!
let testCIImage = CIImage(image: testUIImage) // OK


but this doesn't:



        let testCIImage = CIImage(contentsOf: URL(string: "image_0001.jpg")!)  // returns nil


What am I doing wrong? Is it the URL?







ios swift ciimage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 11:56









rptwsthi

8,52885292




8,52885292










asked Nov 21 '18 at 11:47









thebucc

285




285












  • URL should convey the location of the image not the image name itself
    – iOSer
    Nov 21 '18 at 11:54










  • UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
    – Scriptable
    Nov 21 '18 at 11:54


















  • URL should convey the location of the image not the image name itself
    – iOSer
    Nov 21 '18 at 11:54










  • UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
    – Scriptable
    Nov 21 '18 at 11:54
















URL should convey the location of the image not the image name itself
– iOSer
Nov 21 '18 at 11:54




URL should convey the location of the image not the image name itself
– iOSer
Nov 21 '18 at 11:54












UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
– Scriptable
Nov 21 '18 at 11:54




UIImage(named: "") is a shortcut for looking for images in the assets folder. URL(string: "image_0001.jpg") is not a valid URL and doesn't point to the same thing
– Scriptable
Nov 21 '18 at 11:54












2 Answers
2






active

oldest

votes


















0
















  1. UIImage(named: "name.ext") is a convenience initializer. It's a short form of



    guard let url = Bundle.main.url(forResource:"name", withExtension:"ext"),
    let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else { return nil }
    return image



  2. URL(string: expects a string representing a full URL starting with a scheme for example



    URL(string: "https://example.com/path/to/image_0001.jpg")


    or



    URL(string: "file:///Users/myuser/path/to/image_0001.jpg")







share|improve this answer





















  • thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
    – thebucc
    Nov 22 '18 at 12:18



















3














you need to give file url if your image inside main bundle then you can get url like below code



guard let url = Bundle.main.url(forResource:"image_0001", withExtension:"jpg") else {
return
}
let testCIImage = CIImage(contentsOf: url)





share|improve this answer





















  • this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
    – thebucc
    Nov 22 '18 at 12:25













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411377%2fissue-when-creating-ciimage-from-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0
















  1. UIImage(named: "name.ext") is a convenience initializer. It's a short form of



    guard let url = Bundle.main.url(forResource:"name", withExtension:"ext"),
    let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else { return nil }
    return image



  2. URL(string: expects a string representing a full URL starting with a scheme for example



    URL(string: "https://example.com/path/to/image_0001.jpg")


    or



    URL(string: "file:///Users/myuser/path/to/image_0001.jpg")







share|improve this answer





















  • thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
    – thebucc
    Nov 22 '18 at 12:18
















0
















  1. UIImage(named: "name.ext") is a convenience initializer. It's a short form of



    guard let url = Bundle.main.url(forResource:"name", withExtension:"ext"),
    let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else { return nil }
    return image



  2. URL(string: expects a string representing a full URL starting with a scheme for example



    URL(string: "https://example.com/path/to/image_0001.jpg")


    or



    URL(string: "file:///Users/myuser/path/to/image_0001.jpg")







share|improve this answer





















  • thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
    – thebucc
    Nov 22 '18 at 12:18














0












0








0








  1. UIImage(named: "name.ext") is a convenience initializer. It's a short form of



    guard let url = Bundle.main.url(forResource:"name", withExtension:"ext"),
    let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else { return nil }
    return image



  2. URL(string: expects a string representing a full URL starting with a scheme for example



    URL(string: "https://example.com/path/to/image_0001.jpg")


    or



    URL(string: "file:///Users/myuser/path/to/image_0001.jpg")







share|improve this answer














  1. UIImage(named: "name.ext") is a convenience initializer. It's a short form of



    guard let url = Bundle.main.url(forResource:"name", withExtension:"ext"),
    let data = try? Data(contentsOf: url),
    let image = UIImage(data: data) else { return nil }
    return image



  2. URL(string: expects a string representing a full URL starting with a scheme for example



    URL(string: "https://example.com/path/to/image_0001.jpg")


    or



    URL(string: "file:///Users/myuser/path/to/image_0001.jpg")








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 12:10









vadian

143k13152169




143k13152169












  • thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
    – thebucc
    Nov 22 '18 at 12:18


















  • thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
    – thebucc
    Nov 22 '18 at 12:18
















thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
– thebucc
Nov 22 '18 at 12:18




thanks, your answer solves it. I was just giving the wrong URL. The image is in the file system, added to the project with "Location: Relative to group" (I just dragged & dropped from the file manager). To make the URL valid I not only had to add 'file:///", but also I had to enter the full absolute path: "file:///image_0001.jpg" didn't work, which to me is incomprehensible because UIImage(named: "image_0001.jpg") works.
– thebucc
Nov 22 '18 at 12:18













3














you need to give file url if your image inside main bundle then you can get url like below code



guard let url = Bundle.main.url(forResource:"image_0001", withExtension:"jpg") else {
return
}
let testCIImage = CIImage(contentsOf: url)





share|improve this answer





















  • this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
    – thebucc
    Nov 22 '18 at 12:25


















3














you need to give file url if your image inside main bundle then you can get url like below code



guard let url = Bundle.main.url(forResource:"image_0001", withExtension:"jpg") else {
return
}
let testCIImage = CIImage(contentsOf: url)





share|improve this answer





















  • this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
    – thebucc
    Nov 22 '18 at 12:25
















3












3








3






you need to give file url if your image inside main bundle then you can get url like below code



guard let url = Bundle.main.url(forResource:"image_0001", withExtension:"jpg") else {
return
}
let testCIImage = CIImage(contentsOf: url)





share|improve this answer












you need to give file url if your image inside main bundle then you can get url like below code



guard let url = Bundle.main.url(forResource:"image_0001", withExtension:"jpg") else {
return
}
let testCIImage = CIImage(contentsOf: url)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 12:14









Jatin Kathrotiya

389110




389110












  • this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
    – thebucc
    Nov 22 '18 at 12:25




















  • this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
    – thebucc
    Nov 22 '18 at 12:25


















this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
– thebucc
Nov 22 '18 at 12:25






this one works too, thanks. I'd like to accept BOTH yours and @vadian's answers, because they are both correct but apparently I can't. I'm a noob for swift and frankly I didn't get your answer at first, but I immediately understood vadian's so he gets the tick.
– thebucc
Nov 22 '18 at 12:25




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411377%2fissue-when-creating-ciimage-from-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'