Issue when creating CIImage from file
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
add a comment |
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
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
add a comment |
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
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
ios swift ciimage
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
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")
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
add a comment |
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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")
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
add a comment |
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
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")
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
add a comment |
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
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")
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
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")
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
add a comment |
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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