安卓加载网络图片VSiOS加载网络图片

安卓

记得打开网络权限



Java代码

package com.example.android_load_internet_img;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    private Handler handler;
    Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         imageView=findViewById(R.id.iv);
        new Thread(){
            @Override
            public void run() {//传递给服务器
                bitmap=getBitmap("https://profile.csdnimg.cn/9/9/8/0_wenzhengshi");
              /  bitmap=getHttpBitmap("https://profile.csdnimg.cn/9/9/8/0_wenzhengshi");
                Message m = handler.obtainMessage();
                handler.sendMessage(m);
            }
        }.start();

        handler = new Handler(Looper.getMainLooper()) {

            @Override
            public void handleMessage(Message msg) {
                imageView.setImageBitmap(bitmap);
                //为ListView设置Adapter
                super.handleMessage(msg);
            }

        };


    }



    //第一种方法
    public Bitmap getHttpBitmap(String data)
    {
        Bitmap bitmap = null;
        try
        {
            //初始化一个URL对象
            URL url = new URL(data);
            //获得HTTPConnection网络连接对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5*1000);
            connection.setDoInput(true);
            connection.connect();
            //得到输入流
            InputStream is = connection.getInputStream();
            Log.i("TAG", "*********inputstream**"+is);
            bitmap = BitmapFactory.decodeStream(is);
            Log.i("TAG", "*********bitmap****"+bitmap);
            //关闭输入流
            is.close();
            //关闭连接
            connection.disconnect();
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }

    //第二种方法
    public Bitmap getBitmap(String s)
    {
        Bitmap bitmap = null;
        try
        {
            URL url = new URL(s);
            bitmap = BitmapFactory.decodeStream(url.openStream());
        } catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bitmap;
    }


}

xml布局文件




    




运行效果图

安卓加载网络图片VSiOS加载网络图片_第1张图片

ios

//

//  ViewController.m

//  iOS_load_internet_img

//

//  Created by 温国强 on 2021/12/2.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://profile.csdnimg.cn/9/9/8/0_wenzhengshi"]];

       UIImage *img=[UIImage imageWithData:data];

       UIImageView *imgView=[[UIImageView alloc] initWithImage:img];

       [self.view addSubview:imgView];

      

}

@end

运行效果图

安卓加载网络图片VSiOS加载网络图片_第2张图片 

 

你可能感兴趣的:(android,ios,android,studio)