I want to load an image from a URL.
I can load an image using this code:
local group = display.newGroup();
local testImg;
testImg = display.newImage( "cells/cellBottom.png");
group:insert( testImg );
but i need to use something like:
testImg = display.loadRemoteImage( "https://www.dropbox.com/s/fqlwsa5gupt5rsj/amcCells.png")
group:insert( testImg );
Please tell me how to load this image.
Cheers :)
Just use example given in Corona. This works with the following url but your url seems to have problem.
local function networkListener( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
print ( "event.response.fullPath: ", event.response.fullPath )
print ( "event.response.filename: ", event.response.filename )
print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end
display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, 50, 50 )
try add yougroup:insert(event.target) in listener
local function networkListener( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
**yourgroup:insert(event.target)**
end
print ( "event.response.fullPath: ", event.response.fullPath )
print ( "event.response.filename: ", event.response.filename )
print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end
display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, 50, 50 )
Related
I have a little project that implements NATS Queueing
This is the code:
import * as NATS from '../node_modules/nats' // for typescript
var nats = require('nats');
var config = require('./config');
var opt: NATS.ClientOpts = {'noRandomize': false, 'reconnect': true,
'maxReconnectAttempts': -1, 'servers':config.nat.servers, 'user':
config.nat.user , 'pass': config.nat.pass };
var nc: NATS.Client = nats.connect(opt);
nc.on('error', function(e) {
console.error('Error nats: ' + e);
});
nc.on('close', function() {
console.error('CLOSED nats');
});
nc.subscribe('serviceA', { 'queue': 'A' }, function (request, replyTo) {
console.debug('I exec serviceA via nats:');
j = JSON.parse(request);
console.debug(j);
let ok = {"res": "i am response for service A"}
nc.publish(replyTo, JSON.stringify(ok));
}
let cmd = '{"a": 5, "b": "i am sample"}';
nc.requestOne('serviceA', cmd, {}, 15000, function (response) {
if (response.code && response.code === nats.REQ_TIMEOUT) {
console.error('server timeout');
console.error(response.code);
} else {
console.log('A see this response: ' + response);
}
});
nc.subscribe('serviceB', { 'queue': 'B' }, function (request, replyTo) {
console.debug('I exec serviceB via nats:');
j = JSON.parse(request);
console.debug(j);
let ok = {"res": "i am response for service B"}
nc.publish(replyTo, JSON.stringify(ok));
}
let cmd = '{"c": 5, "d": "i am sample"}';
nc.requestOne('serviceB', cmd, {}, 15000, function (response) {
if (response.code && response.code === nats.REQ_TIMEOUT) {
console.error('server timeout');
console.error(response.code);
} else {
console.log('B see this response: ' + response);
}
});
As you can see, there are 2 services - serviceA on queue A and serviceB on queue B and 2 clients: the first calls service A and the second calls service B
NATS implements Subject ( 'serviceA' and 'serviceB' )
Now, I want try to convert the example using ØMQ I found a similar sample using ZeroMQ
But I can find nothing sample on Subject.
Perhaps ØMQ uses ROUTER so as to implements a subject
Can you help me to implement subject into a ZeroMQ example?
Q: Is it possible to use a Subject in ZeroMQ?A: Yes, it is:
The long story short - one does not need any zmq.ROUTER for doing this, just use a PUB / SUB formal pattern.
Beware: ZeroMQ Socket()-instance is not a tcp-socket-as-you-know-it.
Best
read about the main conceptual differences in [ ZeroMQ hierarchy in less than a five seconds ] Section.
The Publisher side:
import zmq
aCtx = zmq.Context()
aPub = aCtx.Socket( zmq.PUB )
aPub.bind( "tcp://123.456.789.012:3456" )
aPub.setsockopt( zmq.LINGER, 0 )
aPub.setsockopt( zmq.<whatever needed to fine-tune the instance>, <val> )
i = 0
while true:
try:
aPub.send( "ServiceA::[#{0:_>12d}] a Hello World Message.".format( i ) )
aPub.send( "ServiceABCDEFGHIJKLMNOPQRSTUVWXYZ........" )
aPub.send( "ServiceB::[#{0:_>12d}] another message...".format( i / 2 ) ) if ( i == ( 2 * ( i / 2 ) ) ) else pass
sleep( 1 ); i += 1
except KeyboardInterrupt:
print( "---< will exit >---" )
break
print( "---< will terminate ZeroMQ resources >---" )
aPub.close()
aCtx.term()
The Subscriber side:
import zmq
aCtx = zmq.Context()
aSub = aCtx.Socket( zmq.SUB )
aSub.connect( "tcp://123.456.789.012:3456" )
aSub.setsockopt( zmq.LINGER, 0 )
aSub.setsockopt( zmq.SUBSCRIBE, "ServiceA" ) # Subject ( 'serviceA' and 'serviceB' )
aSub.setsockopt( zmq.SUBSCRIBE, "ServiceB" ) # Kindly see the comments below
# # Kindly see API on subscription management details
#
# # Yet another "dimension"
# # to join ingress
# # from multiple sources
#Sub.connect( "<transport-class>://<addr>:<port>" )
# <transport-class :: { inproc | ipc | tcp | pgm | epgm | vmci }
# .connect()-s the same local SUB-AccessPoint to another PUB-side-AccessPoint
# to allow the PUB/SUB Scalable Formal Communication Archetype Pattern
# join a message flow from different source PUB-sides
#Sub.setsockopt( zmq.SUBSCRIBE, "ServiceZ" )
while true:
try:
print( "<<< (((_{0:s}_)))".format( aSub.recv() ) )
except KeyboardInterrupt:
print( "---< will exit >---" )
break
print( "---< will terminate ZeroMQ resources >---" )
aSub.close()
aCtx.term()
I am trying to add a touch event listener to the image object being loaded. Although this is practically an exact copy and paste from the documentation:
https://docs.coronalabs.com/api/type/EventDispatcher/addEventListener.html
It returns the following error:
36: attempt to index local 'object' (a nil value)
local t = {}
local img = {}
local i = 1
local function showImages ()
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
local object = display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end
The table, t, is populated elsewhere in the code and is populated properly.
While you didn't mention which of those lines is line 36 (there are only 28 lines there), I can still see your error. The issue is that object is and always will be nil: display.loadRemoteImage() does not return anything, see this.
What you need to do is have your listener callback capture object, which must be declared before the callback is. The callback should then set the value of object to the results of the download. Like so...
local t = {}
local img = {}
local i = 1
local function showImages ()
local object
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
-- fill in code to save the download object into "object"
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end
I use below code for #1 screenshot.
int _objfnd = ObjectFind( name );
if ( _objfnd == -1 )
{
ObjectCreate ( _vlineName, OBJ_VLINE, 0, Time[i], 0 );
...
}
and I use below code for #2 screenshot.
int _objfnd = ObjectFind( name );
if ( _objfnd == -1 )
{
datetime _dayTime = Time[i] ;
int _dayNext = PeriodSeconds( _Session_Breaks_Day_Prd ) ;
_dayTime = _dayTime - ( _dayTime % _dayNext ) + _dayNext ;
ObjectCreate ( _vlineName, OBJ_VLINE, 0, _dayTime, 0 ) ;
}
If you figure out my concern,please give me good advice, much appreciate.
A: the code has not handled Daylight Saving Time Shift 2016, Nov-06
I've started learning Maxscripts and i've now hit a wall,
im trying to get the name of my selection, if it's a single object and
then if its more than 1, have the label display the number of objects as a string.
but i keep getting an error... any idea?
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
---------------------------------------------------------------------------------------------------------------// Current Selection Function
fn letmeknow obj=
(
local contador = (selection.count as string)
if selection.count != 0 then
(
lbl_01.text = ("Name: " + obj.name)
)
else
(
lbl_01.text = "Nothing Selected"
)
if selection.count >= 2 do (lbl_01.text = ("Objects: " + contador))
)
Looks like the issue is outside the code you've supplied and without seeing the rest of the code, it's hard to tell. Anyway, here's a working example using case expression instead of multiple ifs:
rollout test "Test"
(
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
button btnTest "Test"
fn getSelectionString =
(
case selection.count of
(
0 : "Nothing Selected"
1 : "Name: " + selection[1].name
default : "Objects: " + selection.count as string
)
)
on btnTest pressed do
lbl_01.text = getSelectionString()
)
createDialog test
I using Corona SDK. I parse json and in the listener function I try to load images using links from json, but it says network error. It seems I cannot run network methods in parallel?
Even default corona demo code doesn't work for some reason:
local function networkListener2( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
print ( "RESPONSE: " .. event.response )
end
myImage2 = display.loadRemoteImage(
"http://developer.anscamobile.com/demo/hello.png",
"GET",
networkListener2,
"helloCopy2.png",
system.TemporaryDirectory,
60, 280 )
it fails to run on android emulator.