当前位置: 编程技术>移动开发
本页文章导读:
▪施用xib加载视图后,在dealloc中crash 使用xib加载视图后,在dealloc中crash
最近遇到个问题,就是使用xib加载视图后,经常会在dealloc方法出现crash,在网上找了一下,发现问题所在,详细的参考:
http://stackoverflow.com/questions/61.........
▪ 报错:Message sent to deallocated instance的解决办法 报错:Message sent to deallocated instance的解决方法
NSMutableArray *arr = [NSMutableArray array];
[NSMutableArray array]相当于[[[NSMutableArray alloc] init] autorelease]. 所以有一个隐式的释放 (autorelease) 。
以下是保存.........
▪ 选择照片,设置头像,下传头像 选择照片,设置头像,上传头像
// 选择照片
private void chooseImage() {
final CharSequence[] items = { "从相册选择", "拍照" };
AlertDialog dlg = new AlertDialog.Builder(CreateCardActivity.this)
.setTitle("选择照片").........
[1]施用xib加载视图后,在dealloc中crash
来源: 互联网 发布时间: 2014-02-18
使用xib加载视图后,在dealloc中crash
最近遇到个问题,就是使用xib加载视图后,经常会在dealloc方法出现crash,在网上找了一下,发现问题所在,详细的参考:
http://stackoverflow.com/questions/61838/do-i-need-to-release-xib-resources
总结一下:
使用IBoutlet进行retain后,是需要在dealloc中进行release的,但加上XXX = nil;会解决你的crash问题。具体原因,查看上面stackoverflow所述
[2] 报错:Message sent to deallocated instance的解决办法
来源: 互联网 发布时间: 2014-02-18
报错:Message sent to deallocated instance的解决方法
NSMutableArray *arr = [NSMutableArray array];
[NSMutableArray array]相当于[[[NSMutableArray alloc] init] autorelease]. 所以有一个隐式的释放 (autorelease) 。
以下是保存他的方法:
arr = [[NSMutableArray array] retain];
arr = [[NSMutableArray alloc] init];
利用setter能保存新值得方法 (声明@property(retain)):
self.arr = [NSMutableArray array];
如果有不同观点希望能够提提意见建议,谢谢!
[3] 选择照片,设置头像,下传头像
来源: 互联网 发布时间: 2014-02-18
选择照片,设置头像,上传头像
// 选择照片 private void chooseImage() { final CharSequence[] items = { "从相册选择", "拍照" }; AlertDialog dlg = new AlertDialog.Builder(CreateCardActivity.this) .setTitle("选择照片") .setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { // 这里item是根据选择的方式,在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法 if (item == 1) { Intent getImageByCamera = new Intent( "android.media.action.IMAGE_CAPTURE"); startActivityForResult(getImageByCamera, 1); } else { Intent getImage = new Intent( Intent.ACTION_GET_CONTENT); getImage.addCategory(Intent.CATEGORY_OPENABLE); getImage.setType("image/jpeg"); startActivityForResult(getImage, 0); } } }).create(); dlg.show(); } // 得到选择的照片结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { ContentResolver resolver = getContentResolver(); byte[] mContent; Bitmap myBitmap = null; /** * 因为两种方式都用到了startActivityForResult方法,这个方法执行完后都会执行onActivityResult方法, * 所以为了区别到底选择了那个方式获取图片要进行判断,这里的requestCode跟startActivityForResult里面第二个参数对应 */ if (requestCode == 0) { try { //获得图片的uri Uri originalUri = data.getData(); //将图片内容解析成字节数组 mContent=readStream(resolver.openInputStream(Uri.parse(originalUri.toString()))); //将字节数组转换为ImageView可调用的Bitmap对象 myBitmap = getPicFromBytes(mContent, null); ////把得到的图片绑定在控件上显示 img_head.setImageBitmap(myBitmap); } catch (Exception e) { System.out.println(e.getMessage()); } }else if(requestCode ==1){ try { super.onActivityResult(requestCode, resultCode, data); Bundle extras = data.getExtras(); myBitmap = (Bitmap) extras.get("data"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); myBitmap.compress(Bitmap.CompressFormat.JPEG , 100, baos); mContent=baos.toByteArray(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //把得到的图片绑定在控件上显示 img_head.setImageBitmap(myBitmap); } } public static Bitmap getPicFromBytes(byte[] bytes, BitmapFactory.Options opts) { if (bytes != null) if (opts != null) return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,opts); else return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); return null; } public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; } // 上传照片 private void uploadHeadImage() throws IOException{ httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost(Constant.uploadHeadUrl+"id=1"); Drawable drawable = img_head.getDrawable(); Bitmap bmp = ((BitmapDrawable)drawable).getBitmap(); FileOutputStream fop = new FileOutputStream("/mnt/sdcard/temp/kkpy_head.jpg"); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fop); //压缩bitmap写进outputStream 参数:输出格式 输出质量 目标OutputStream //格式可以为jpg,png,jpg不能存储透明 fop.close(); File file = new File("/mnt/sdcard/temp/kkpy_head.jpg"); FileEntity reqEntity = new FileEntity(file, "binary/octet-stream"); httppost.setEntity(reqEntity); //httppost.getParams().setParameter("id", "1"); reqEntity.setContentType("binary/octet-stream"); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); }
最新技术文章: