从远程LSR文件创建UIImage

时间:2015-10-28 17:03:16

标签: swift uiimage tvos apple-tv

我正在尝试使用Apple远程服务器上托管的Apple新分层图像文件创建一个UIImage。

下面的示例代码正确下载了lsr文件(data var保存了一个值),但是使用它创建一个新的NSImage会产生一个nil值。忽略这个代码同步且效率低的事实。

if let url = NSURL(string: "http://path/to/my/layered/image.lsr") {
    if let data = NSData(contentsOfURL: url) {
        let image = UIImage(data: data) // `image` var is nil here
        imageView?.image = image
    }
}

有关如何下载LSR并使用它创建UIImage的想法吗?

1 个答案:

答案 0 :(得分:2)

这就是我解决它的方式:

  1. 将.lsr文件转换为.lcr文件,从控制台执行此操作: class UF { int *id, cnt, *sz; public: // Create an empty union find data structure with N isolated sets. UF(int N) { cnt = N; id = new int[N]; sz = new int[N]; for (int i = 0; i<N; i++) id[i] = i, sz[i] = 1; } ~UF() { delete[] id; delete[] sz; } // Return the id of component corresponding to object p. int find(int p) { int root = p; while (root != id[root]) root = id[root]; while (p != root) { int newp = id[p]; id[p] = root; p = newp; } return root; } // Replace sets containing x and y with their union. void merge(int x, int y) { int i = find(x); int j = find(y); if (i == j) return; // make smaller root point to larger one if (sz[i] < sz[j]) { id[i] = j, sz[j] += sz[i]; } else { id[j] = i, sz[i] += sz[j]; } cnt--; } // Are objects x and y in the same set? bool connected(int x, int y) { return find(x) == find(y); } // Return the number of disjoint sets. int count() { return cnt; } };
  2. 在您的服务器上上传your_file.lcr
  3. 将这两个函数放入ut​​il类中:

    package Test.TestArtifact;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Store;
    
    
    public class ExecutorServiceExample {
        public static void main(String[] args) throws MessagingException {
            ExecutorServiceExample eService = new ExecutorServiceExample();
            Folder inbox = eService.getInboxFolder();
            int count = eService.getMailCount();
            Message messages = inbox.getMessage(count);
            System.out.println("Total Mail count = " + count);
    
            ExecutorService executor = Executors.newFixedThreadPool(5);
            int waitTime = 1000000;
    
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    
            Date startDate;
            Date endDate;
            try {
                startDate = formatter.parse("2015-10-01");
                endDate = formatter.parse("2015-10-31");
                while(startDate.before(endDate)){
                    for(int j=0;j<5;j++){
                        Runnable runner = new TaskPrint(startDate,messages);
                        executor.execute(runner);
                    }
                }
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
    
    
            try {
                Thread.sleep(waitTime);
                executor.shutdown();
                executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
    
            System.exit(0);
        }
    
        private Folder getInboxFolder() throws MessagingException{
            Properties props = System.getProperties();
            props.setProperty("mail.store.protocol", "imap");
            Session session = Session.getInstance(props);
            Store store = session.getStore("imap");
            store.connect("", "", "");
            store.getFolder("INBOX").open(Folder.READ_ONLY);
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            return inbox;       
        }
    
        private int getMailCount() throws MessagingException{
            int count = getInboxFolder().getMessageCount();
            return count;
    
        }
    
    
    }
    
  4. 像这样使用:

    xcrun --sdk appletvos layerutil --c your_file.lsr
  5. 这将使用图像而不将其保存到文档目录,如果您需要该解决方案,请询问我并分享。