1
votes

I develop an application using WatchKit 2. I have to transfer a image from iOS device to watch. I sent the image url. This is my code on watch extension side that i used to set image on my WKInterfaceImage:

       NSError *error = nil;
       NSData * dataq = [NSData dataWithContentsOfURL:[NSURL URLWithString:"url string"] options:NSDataReadingMappedAlways error:&error];
       UIImage *img = [UIImage imageWithData:dataq];
       [cell.babyImage  setImage:formattedImage];

On simulator (watch simulator and watch simulator) it works but on real devices (iPhone and watch) i got error :

error =

Error Domain=NSCocoaErrorDomain Code=256 "The file “image.jpg” couldn’t be opened." UserInfo={NSURL=https://"image url"}

1

1 Answers

3
votes

I had a similar issue just to download an image to the watch with NSData, but I didn't transfer it from the iPhone. The image also was not displayed on the real watch, but in the simulator.

I just switched to NSUrlSession.dataTaskWithURL and it worked:

        func loadImage(imageUrl: String, forImageView: WKInterfaceImage) {

            NSURLSession.sharedSession().dataTaskWithURL(imageUrl, completionHandler: {(data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in                         
                if let image = UIImage(data: data!) {
                    dispatch_async(dispatch_get_main_queue()) {
                        forImageView.setImage(image)
                    }
                } else {
                    NSLog("could not load data from image URL: \(imageUrl)")
                }
            }).resume()
        }